#K53737. Rotate Matrix 90 Degrees Clockwise
Rotate Matrix 90 Degrees Clockwise
Rotate Matrix 90 Degrees Clockwise
You are given a square matrix of size \(n \times n\). Your task is to rotate this matrix by 90° clockwise in-place. That means you should perform the rotation without using extra space for another matrix.
The matrix rotation is done by first transposing the matrix and then reversing each row. For example, given the following matrix:
[ \begin{bmatrix} 1 & 2 & 3 \ 4 & 5 & 6 \ 7 & 8 & 9 \end{bmatrix} ]
The rotated matrix will be:
[ \begin{bmatrix} 7 & 4 & 1 \ 8 & 5 & 2 \ 9 & 6 & 3 \end{bmatrix} ]
</p>Note: The rotation must be done in-place.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains a single integer \(n\), representing the number of rows and columns of the matrix.
- The next \(n\) lines each contain \(n\) space-separated integers representing a row of the matrix.
outputFormat
Output the rotated matrix to standard output (stdout) in the same format as the input. Each row should be printed on a new line and the integers in each row should be space-separated.
## sample3
1 2 3
4 5 6
7 8 9
7 4 1
8 5 2
9 6 3
</p>