You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Visual:
1 2 3 7 4 1
4 5 6 => 8 5 2
7 8 9 9 6 3
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Visual:
5 1 9 11 15 13 2 5
2 4 8 10 => 14 3 4 1
13 3 6 7 12 6 8 9
15 14 12 16 16 7 10 11
Input: matrix = [[1]]
Output: [[1]]
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
Visual:
1 2 3 1
3 4 => 4 2
n == matrix.length == matrix[i].length1 <= n <= 20-1000 <= matrix[i][j] <= 1000There are two main approaches to rotate a matrix 90° clockwise in-place:
Key Insight: Rotate the matrix from the outermost layer to the innermost layer.
Think of the matrix as concentric squares (layers). For each layer:
Algorithm:
For each layer (from 0 to n/2):
For each cell in the layer (excluding corners to avoid double-processing):
Save top cell
Top ← Left
Left ← Bottom
Bottom ← Right
Right ← Saved top
Why this works:
Key Insight: 90° clockwise = Transpose + Reverse each row
Algorithm:
1. Transpose the matrix (swap matrix[i][j] with matrix[j][i])
2. Reverse each row
Example:
Original: Transposed: After Reversing Rows:
1 2 3 1 4 7 7 4 1
4 5 6 => 2 5 8 => 8 5 2
7 8 9 3 6 9 9 6 3
Why this works:
Time Complexity: O(n²)
Space Complexity: O(1)
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// Rotate layer by layer
for (int layer = 0; layer < n / 2; layer++) {
int first = layer;
int last = n - 1 - layer;
for (int i = first; i < last; i++) {
int offset = i - first;
// Save top
int top = matrix[first][i];
// Left -> Top
matrix[first][i] = matrix[last - offset][first];
// Bottom -> Left
matrix[last - offset][first] = matrix[last][last - offset];
// Right -> Bottom
matrix[last][last - offset] = matrix[i][last];
// Top -> Right
matrix[i][last] = top;
}
}
}
}
def rotate(matrix: List[List[int]]) -> None:
n = len(matrix)
# Rotate layer by layer
for layer in range(n // 2):
first = layer
last = n - 1 - layer
for i in range(first, last):
offset = i - first
# Save top
top = matrix[first][i]
# Left -> Top
matrix[first][i] = matrix[last - offset][first]
# Bottom -> Left
matrix[last - offset][first] = matrix[last][last - offset]
# Right -> Bottom
matrix[last][last - offset] = matrix[i][last]
# Top -> Right
matrix[i][last] = top
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// 1. Transpose
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// 2. Reverse each row
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n - 1 - j];
matrix[i][n - 1 - j] = temp;
}
}
}
}
def rotate(matrix: List[List[int]]) -> None:
n = len(matrix)
# 1. Transpose
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# 2. Reverse each row
for i in range(n):
matrix[i].reverse()
For a 4×4 matrix, here's how layer-by-layer works:
Layer 0 (outer):
* * * *
* *
* *
* * * *
Layer 1 (inner):
* *
* *
Each layer is rotated independently, processing 4 cells at a time.
This problem demonstrates:
< last not <= last)j = 0 instead of j = i + 1)| Approach | Time | Space | Code Complexity | Intuition | |----------|------|-------|-----------------|-----------| | Layer-by-Layer | O(n²) | O(1) | Medium | Harder to understand | | Transpose + Reverse | O(n²) | O(1) | Easy | Easier to remember |
Recommendation: Learn both! Transpose+Reverse is easier to code in an interview, but Layer-by-Layer shows deeper understanding of matrix manipulation.
Q: How would you rotate counter-clockwise?
Q: What if you can use extra space?
Q: How to check if a matrix is a rotation of another?
Q: Can you do better than O(n²)?