Rotate Matrix
Given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly without using another 2D matrix.
[ [ 1, 2 ], [ 3, 4 ] ]
Explanation. Rotating the matrix [[1,2],[3,4]] 90 degrees clockwise results in [[3,1],[4,2]].
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
Explanation. Rotating the matrix [[1,2,3],[4,5,6],[7,8,9]] 90 degrees clockwise results in [[7,4,1],[8,5,2],[9,6,3]].
[ [ 1 ] ]
Explanation. A 1x1 matrix rotated 90 degrees remains the same.
Follow-up: Can you perform this operation in other directions like 90 degrees counterclockwise or 180 degrees?
1. The given matrix will be of size n x n, where n is greater than 0. 2. You must rotate the matrix in-place; using extra space for another matrix is not allowed.
- Views
- 3