Rotate Image
Rotate an n x n 2D matrix 90 degrees clockwise in-place.
Input: matrix, a list of n lists of n integers (the example shows it as rows of comma-separated integers). Modify matrix in place and return nothing; the tests print the rotated matrix afterwards, one row per line with values separated by spaces.
Example
1,2,3 4,5,6 7,8,9
7 4 1 8 5 2 9 6 3
matrixis a square grid with rows and 3 columns: rows1,2,3,4,5,6,7,8,9. Count rows and columns from 0.- A 90° clockwise turn sends the entry at row , column to row , column . Here that column is .
- Track the top row:
1at lands at ,2at lands at ,3at lands at . The top row becomes the right column. - Track the left column:
7at lands at and4at lands at , so the new top row is7 4 1. Every other cell follows the same rule, giving8 5 2and9 6 3below it. - Notice that the four corners trade places in a cycle ( by position), and the centre
5stays put because maps to itself. - The grid itself must hold the rotated values; the printed rows are read from it.
Constraints
- 1 <= n <= 20
- -1000 <= matrix[i][j] <= 1000