Intermediate Level

Implement the rotateImageClockwise method that returns the matrix after rotating it 90 degrees clockwise.

The input contains a square matrix matrix of size n × n.

Your task is to rotate the matrix 90 degrees clockwise and return the rotated matrix.

For example, [[1,2],[3,4]] becomes [[3,1],[4,2]] after a clockwise rotation.

Example 1
Input:
matrix (int[][]) = [[1,2],[3,4]]
n (int) = 2
Return:
(int[][]) [[3,1],[4,2]]
Example 2
Input:
matrix (int[][]) = [[1]]
n (int) = 1
Return:
(int[][]) [[1]]
Example 3
Input:
matrix (int[][]) = [[1,2,3],[4,5,6],[7,8,9]]
n (int) = 3
Return:
(int[][]) [[7,4,1],[8,5,2],[9,6,3]]

A simple approach is to create a new matrix and place every value in its rotated position.

For a value at position [row][col] in the original matrix, its clockwise rotated position is [col][n - 1 - row] in the result matrix.

Fill the result matrix using this formula and return it.

Pseudocode:

function rotateImageClockwise(matrix, n):
    result = create n by n matrix
    for row from 0 to n - 1:
        for col from 0 to n - 1:
            newRow = col
            newCol = n - 1 - row
            result[newRow][newCol] = matrix[row][col]
    return result
Run your code to see the result.