Rotate Image
Given an n x n matrix, rotate it 90 degrees clockwise in place, modifying the input matrix directly without allocating another 2D matrix.
Open official problem prompt ↗Turn the entire square grid a quarter turn clockwise using only swaps inside the given matrix.
Like rotating a physical photo on a table: first flip it along its diagonal crease, then flip each horizontal strip end-for-end, and it ends up turned 90 degrees.
- Input
- matrix = [[1,2,3],[4,5,6],[7,8,9]]
- Output
- [[7,4,1],[8,5,2],[9,6,3]]
- Why
- The first column bottom-to-top (7,4,1) becomes the first row, which is exactly a 90-degree clockwise rotation.
n == matrix.length == matrix[i].length1 <= n <= 20-1000 <= matrix[i][j] <= 1000