[Medium] 48. Rotate Image
This is a matrix manipulation problem that requires rotating a 2D matrix 90 degrees clockwise in-place. The key insight is understanding the relationship between matrix positions during rotation and implementing it efficiently.
Given an n x n 2D matrix representing an image, rotate the image by 90 degrees clockwise in-place.
Examples
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:
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]]
Constraints
- n == matrix.length == matrix[i].length
- 1 <= n <= 20
- -1000 <= matrix[i][j] <= 1000
Thinking Process
There are two main approaches to solve this problem:
- Direct Rotation: Rotate elements in groups of 4 using coordinate mapping
- Transpose + Reflect: Transpose the matrix then reflect each row
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Row/column traversal | O(nm) | O(1) | Simulation, spiral |
| BFS/DFS on grid | O(nm) | O(nm) | Islands, shortest path |
| Matrix as graph | O(nm) | O(nm) | 4/8-directional neighbors |
| Transpose / rotate (this problem) | O(nm) | O(1) | In-place rotation tricks |
Solution
Time Complexity: O(n²) - Visit each element once
Space Complexity: O(1) - Only using constant extra space
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
for (int[] row : matrix) {
for (int l = 0, r = row.length - 1; l < r; l++, r--) {
int tmp = row[l];
row[l] = row[r];
row[r] = tmp;
}
}
}
}```
### Solution Explanation
**Approach:** Transpose / rotate (this problem)
**Key idea:** There are two main approaches to solve this problem:
**How the code works:**
1. **Direct Rotation**: Rotate elements in groups of 4 using coordinate mapping
2. **Transpose + Reflect**: Transpose the matrix then reflect each row
**Walkthrough** — input `matrix = [[1,2,3],[4,5,6],[7,8,9]]`, expected output `[[7,4,1],[8,5,2],[9,6,3]]`:
1. Initialize variables from the problem setup.
2. Apply the main loop / recursion until the condition is met.
3. Confirm the result matches the expected output.
## Step-by-Step Example
Let's trace through Solution 2 with matrix = `[[1,2,3],[4,5,6],[7,8,9]]`:
**Step 1: Transpose**
Original: [1,2,3] Transposed: [1,4,7] [4,5,6] [2,5,8] [7,8,9] [3,6,9]
**Step 2: Reflect (reverse each row)**
Transposed: [1,4,7] Reflected: [7,4,1] [2,5,8] [8,5,2] [3,6,9] [9,6,3] ```
Result: [[7,4,1],[8,5,2],[9,6,3]]
Coordinate Mapping (Solution 1)
For a 90° clockwise rotation, the coordinate transformation is:
(i, j) → (j, n-1-i)
The four positions that rotate together:
(i, j)→(j, n-1-i)(j, n-1-i)→(n-1-i, n-1-j)(n-1-i, n-1-j)→(n-1-j, i)(n-1-j, i)→(i, j)
Matrix Size Considerations
- Even n: Process all n²/4 groups
- Odd n: Process (n²-1)/4 groups (center element stays unchanged)
Common Mistakes
- Coordinate Errors: Incorrect mapping formulas
- Boundary Issues: Not handling odd matrix sizes correctly
- Over-rotation: Processing the same elements multiple times
- Index Confusion: Mixing up row and column indices
References
- LC 48: Rotate Image on LeetCode
- LeetCode Discuss — LC 48: Rotate Image
- LeetCode Editorial (may require premium)
Key Takeaways
- In-Place Rotation: Must modify the original matrix without extra space
- Group of 4: Each element participates in a cycle of 4 positions
- Boundary Handling: Careful with odd/even matrix sizes
- Mathematical Approach: Transpose + reflect is more intuitive