You are given an
n x n
2D-matrix. Write a program to
rotate the matrix by 90 degrees
.
Problem Note
- The rotation should be in a clockwise direction.
- The rotation should be in-place, which means you have to modify the input 2D-matrix and not to use some other 2D-matrix.
Example 1
Input:
[
[7, 8, 9],
[1, 2, 3],
[4, 5, 6]
]
Output: Rotate the input matrix in-place such that it becomes:
[
[4, 1, 7],
[5, 2, 8],
[6, 3, 9]
]
Explanation: In the above example, when the input matrix is rotated by 90 degrees clockwise, the elements of the 1st column have come 1st row, 2nd column to 2nd row, and 3rd column to 3rd row respectively of the resultant matrix.
Example 2
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
Output: Rotate the input matrix in-place such that it becomes:
[
[13, 9, 5, 1],
[14, 10, 6, 2],
[15, 11, 7, 3],
[16, 12, 8, 4]
]
Explanation: In the above example, when the input matrix is rotated by 90 degrees clockwise, the elements of the 1st column have come 1st row, 2nd column to 2nd row, 3rd column to 3rd row, and 4th column to 4th the 4th row respectively of the resultant matrix.