Rotate an matrix 90 degrees clockwise in place.
Given an matrix of integers, rotate the matrix 90 degrees clockwise in place.
You must modify the input matrix directly and use only extra space.
A clockwise rotation changes the position of every element according to its row and column indices. The task is to update the matrix without creating a second matrix.
matrix of size .matrix[i][j] is an integer value.matrix in place so that it represents the same matrix rotated 90 degrees clockwise.Example 1
Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[[7,4,1],[8,5,2],[9,6,3]]
Explanation
After rotating 90 degrees clockwise, the first row becomes the first column from bottom to top.
Example 2
Input
matrix = [[5,1],[9,11]]
Output
[[9,5],[11,1]]
Explanation
Each element shifts to its rotated position within the 2x2 matrix.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.