Rotate an matrix 90 degrees clockwise in place.
Problem
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.
Intuition
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.
Input Format
- A square matrix
matrixof size . matrix[i][j]is an integer value.
Output Format
- Modify
matrixin place so that it represents the same matrix rotated 90 degrees clockwise. - No separate return value is required beyond the mutated matrix state.
Constraints
- The matrix is square.
- Use extra space.
- The matrix values fit in standard 32-bit signed integers.
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
Unlock deeper context for this problem
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.