Rotate Matrix
matrix is an n × n grid of numbers. Rotate it 90 degrees clockwise and return it: the first row becomes the last column, the second row the column before it, and so on.
Try rotating in place, without a second grid.
Constraints
- 1 ≤
n≤ 100 - -1,000 ≤
matrix[r][c]≤ 1,000
Example 1
Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[[7,4,1],[8,5,2],[9,6,3]]
Why:
The top row 1, 2, 3 becomes the right column, read from top to bottom.
Example 2
Input
matrix = [[1,2],[3,4]]
Output
[[3,1],[4,2]]
Why:
A 2 × 2 grid.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –