Rotate Image
Medium · Matrices
Jump to codeRotate a square matrix 90 degrees clockwise in place. Do not allocate another matrix.
Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2
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
Function contract
Modify matrix in place. The grader checks its value after your function returns.
Need a hint?
Two simple reflections compose a rotation.
Asked in real interviews
Candidates reported versions of Rotate Image from these company interviews. Some company questions are locked and open with Premium or XP.
- Rotate a Matrix In PlaceApple
- Implement Cache and Rotate MatrixAmazon
- Rotate matrix and find max lamp coverageCapital One
Solution approach
Transpose the matrix by swapping entries across the main diagonal, touching each pair once. Then reverse every row in place. An original entry at (r,c) ends at (c,n−1−r), which is the clockwise rotation.
Complexity
O(n²) time and O(1) extra space.
Python reference solution
def rotate(matrix):
n = len(matrix)
for row in range(n):
for col in range(row + 1, n):
matrix[row][col], matrix[col][row] = matrix[col][row], matrix[row][col]
for row in matrix:
row.reverse()
JavaScript solution
function rotate(matrix) {
const n = matrix.length;
for (let row = 0; row < n; row++) {
for (let col = row + 1; col < n; col++) {
[matrix[row][col], matrix[col][row]] = [matrix[col][row], matrix[row][col]];
}
}
for (const row of matrix) row.reverse();
}
Java solution
import java.util.*;
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int row = 0; row < n; row++) {
for (int col = row + 1; col < n; col++) {
int value = matrix[row][col];
matrix[row][col] = matrix[col][row]; matrix[col][row] = value;
}
}
for (int[] row : matrix) {
for (int left = 0, right = n - 1; left < right; left++, right--) {
int value = row[left]; row[left] = row[right]; row[right] = value;
}
}
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void rotate(vector<vector<int>> &matrix) {
int n = matrix.size();
for (int row = 0; row < n; row++) {
for (int col = row + 1; col < n; col++) swap(matrix[row][col], matrix[col][row]);
}
for (auto& row : matrix) reverse(row.begin(), row.end());
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 8 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
[
1,
2,
3
],
[
4,
5,
6
],
[
7,
8,
9
]
]
][
[
7,
4,
1
],
[
8,
5,
2
],
[
9,
6,
3
]
][
[
[
5,
1,
9,
11
],
[
2,
4,
8,
10
],
[
13,
3,
6,
7
],
[
15,
14,
12,
16
]
]
][
[
15,
13,
2,
5
],
[
14,
3,
4,
1
],
[
12,
6,
8,
9
],
[
16,
7,
10,
11
]
][
[
[
1
]
]
][
[
1
]
][
[
[
1,
2
],
[
3,
4
]
]
][
[
3,
1
],
[
4,
2
]
][
[
[
0,
-1
],
[
2,
3
]
]
][
[
2,
0
],
[
3,
-1
]
]Drafts are saved per language in this browser when storage is available.