Rotate Image
Pattern: Matrix traversal
Turn an n × n matrix 90 degrees clockwise in place. Swap across the main diagonal (a transpose), then reverse every row.
Solve on LeetCodeSolve on GeeksforGeeks
The problem
You are given an n × n matrix. Rotate it 90 degrees clockwise, in place, without making a second matrix. For example,
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] becomes [[7, 4, 1], [8, 5, 2], [9, 6, 3]].
The function name rotate matches LeetCode. Use the link above for the exact statement and limits. The GFG link covers
the same problem.
Think first
- After the turn, where does the cell at row
i, columnjend up? - Which simple in-place operation swaps rows with columns?
- What is left to do after that?
Hints
Brute force, and why it fails
Copy into a new matrix with new[j][n - 1 - i] = old[i][j], then copy back: O(n²) time and O(n²) extra memory. The
rule asks for no second matrix. Rotating four cells at a time in rings also works in O(1) memory but is easier to get
wrong.
The key insight
A rotation is two reflections: one across the main diagonal and one across the vertical middle line. Each reflection is a set of plain swaps, so both work in place.
Dry run
The matrix rows are 1 2 3, 4 5 6 and 7 8 9. After a clockwise turn, the first column read upwards (7 4 1) becomes the first row.
The plan
for i from 0 to n - 1:
for j from i + 1 to n - 1:
swap matrix[i][j], matrix[j][i]
for each row in matrix:
reverse row
Complexity
- Time
O(n²)- Space
O(1)
The transpose swaps each pair once and the reversals touch each cell once. Only one temporary value is used.
Edge cases
1 × 1: nothing changes.2 × 2:[[1, 2], [3, 4]]becomes[[3, 1], [4, 2]].- Negative values or repeats change nothing about the method.
I've built my logic, show the code
Java
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) { // 1. transpose: swap across the main diagonal
for (int j = i + 1; j < n; j++) {
int t = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = t;
}
}
for (int[] row : matrix) { // 2. reverse every row
for (int l = 0, r = n - 1; l < r; l++, r--) {
int t = row[l]; row[l] = row[r]; row[r] = t;
}
}
}
}C++
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = static_cast<int>(matrix.size());
for (int i = 0; i < n; i++) // 1. transpose: swap across the main diagonal
for (int j = i + 1; j < n; j++) swap(matrix[i][j], matrix[j][i]);
for (vector<int>& row : matrix) reverse(row.begin(), row.end()); // 2. reverse every row
}
};Python
class Solution:
def rotate(self, matrix: list[list[int]]) -> None:
n = len(matrix)
for i in range(n): # 1. transpose: swap across the main diagonal
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
for row in matrix: # 2. reverse every row
row.reverse()Common mistakes
- Starting the inner loop at
j = 0, which swaps every pair twice and undoes the transpose. - Reversing columns instead of rows, which turns the matrix anticlockwise.
- Building a new matrix and pointing the variable at it. The caller's matrix does not change.