#C1322. Rotate Matrix 90° Clockwise
Rotate Matrix 90° Clockwise
Rotate Matrix 90° Clockwise
You are given an n x n matrix. Your task is to rotate the matrix 90° clockwise in place. In other words, you should modify the original matrix without using additional space for another matrix.
The typical approach involves two steps:
- Transpose the matrix (swap rows with columns).
- Reverse each row of the transposed matrix.
For example, if the input matrix is:
1 2 3 4
After rotating, it becomes:
3 1 4 2
Apply the same method for larger matrices.
inputFormat
The input is read from stdin and has the following format:
- The first line contains a single integer
n
, the size of the matrix. - The following
n
lines each containn
space-separated integers representing a row of the matrix.
outputFormat
Output the rotated matrix to stdout in the following format:
- There should be
n
lines, each containingn
space-separated integers that represent the rows of the matrix after the rotation.
2
1 2
3 4
3 1
4 2
</p>