Rotate a Matrix by 90 Degrees
Updated
Problem statement
You are given a matrix matrix with n rows and m columns. Rotate it 90 degrees clockwise and return the rotated matrix.
The rotated matrix has m rows and n columns. The first row of matrix becomes the last column of the result, the second row becomes the second-to-last column, and so on. Because the shape can change, return a new matrix instead of changing matrix in place.
Function
rotateMatrix(matrix: int[][]) → int[][]Examples
Example 1
matrix = [[1,2,3],[4,5,6]]return = [[4,1],[5,2],[6,3]]The input has 2 rows and 3 columns, so the result has 3 rows and 2 columns. The first row [1,2,3] becomes the last column, read from top to bottom. The second row [4,5,6] becomes the first column.
Example 2
matrix = [[1,2],[3,4]]return = [[3,1],[4,2]]For a square matrix the shape stays the same. Row [1,2] becomes the right column and row [3,4] becomes the left column.
Example 3
matrix = [[7],[8],[9]]return = [[9,8,7]]A single column of 3 rows becomes a single row. The bottom value 9 comes first, because the last row of the input becomes the first column of the result.
Constraints
n == matrix.lengthm == matrix[i].length1 <= n, m <= 50-1000 <= matrix[i][j] <= 1000