Rotate Matrix
Given a square matrix (2D array) of integers, rotate the matrix by 90 degrees clockwise. You should perform this operation in-place, modifying the matrix directly. Do not use another matrix for the rotation process.
[ [ 1, 2 ], [ 3, 4 ] ]
Explanation. Rotating the matrix clockwise by 90 degrees repositions the elements as follows. Top row becomes the right column and bottom row becomes the left column.
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
Explanation. Each element in the matrix is shifted according to its new position in a clockwise 90-degree rotation.
[ [ 1 ] ]
Explanation. A single-element matrix remains unchanged after any rotation.
Follow-up: How would you modify your solution if the rotation needs to be counterclockwise?
1. The given matrix will be a non-empty square matrix, i.e., the number of rows will be equal to the number of columns.\n2. The matrix will contain at least one element and at most (10 x 10) elements.\n3. Matrix elements will consist of integers.
- Views
- 2