Home
DSA Patterns
Matrices
Rotate Image
Rotate Image (LeetCode 48)
Rotate Image (LeetCode 48): rotate the matrix by 90 degrees clockwise in-place.
Problem Understanding
Turn an n x n matrix, an image stored as a grid of pixels, a quarter turn clockwise. The rotation must happen in-place, inside the same matrix, without allocating a second one.
Strategy (Transpose + Reflect)
- Transpose: Swap
matrix[i][j]withmatrix[j][i]. - Reflect: Reverse each row.
Step 1: Transpose converts rows to columns.
Step 2: Reversing rows fixes the left-right order for 90 deg rotation.
Interactive Visualization
READY
0
1
2
0
1
2
Size
3 × 3
Swaps
0
n = 3. First transpose: swap each cell above the diagonal with its mirror below it (the diagonal swaps with itself, so it never moves)
Next
Swap matrix[0][1] and matrix[1][0]: 2 ↔ 4
Matrix
See the two-step process: Transpose then Reverse.
Dry Run
Matrix: [[1,2],[3,4]]
| Step | Action | State |
|---|---|---|
| 1 | Transpose | [[1,3],[2,4]] (Swap 2 & 3) |
| 2 | Reverse Row 0 | [[3,1],[2,4]] |
| 3 | Reverse Row 1 | [[3,1],[4,2]] |
Result: 90 deg clockwise rotation.
Edge Cases & Checks
Validation points:
Square Matrix: Problem usually guarantees n x n. If not, this method fails.
Clockwise vs Counter: For Counter-Clockwise, Reverse then Transpose.
Solution
O(N^2) Time, O(1) Space.
Rotate Image Code
1var rotate = function(matrix) {2 const n = matrix.length;3 // Transpose4 for (let i = 0; i < n; i++) {5 for (let j = i; j < n; j++) {6 [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];7 }8 }9 // Reflect10 for (let i = 0; i < n; i++) {11 matrix[i].reverse();12 }13};
Rotate Image in Python, Java, C++, TypeScript and C
Rotate Image in Python
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
n = len(matrix)
# Transpose
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Reflect (Reverse rows)
for i in range(n):
matrix[i].reverse()Rotate Image in Java
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// Transpose
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Reflect
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;
}
}
}
}Rotate Image in C++
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
// Transpose
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
// Reflect
for (int i = 0; i < n; i++) {
reverse(matrix[i].begin(), matrix[i].end());
}
}
};Rotate Image in TypeScript
function rotate(matrix: number[][]): void {
const n = matrix.length;
// Transpose
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
const temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Reflect
for (let i = 0; i < n; i++) {
matrix[i].reverse();
}
};Rotate Image in C
void rotate(int** matrix, int matrixSize, int* matrixColSize) {
int n = matrixSize;
// Transpose
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Reflect
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;
}
}
}More practice: DSA interview questions with answers · patterns cheat sheet
