Rotate Image
Problem
n × n grid of integers representing an image.
Turn the image 90° clockwise, changing the grid in-place — you may not
build and return a second matrix.[[1,2,3],[4,5,6],[7,8,9]]Output[[7,4,1],[8,5,2],[9,6,3]][[1,2],[3,4]]Output[[3,1],[4,2]]n == matrix.length == matrix[i].length · 1 ≤ n ≤ 20 · -1000 ≤ matrix[i][j] ≤ 1000 · In-place only.
Try it yourself
class Solution {
public void rotate(int[][] matrix) {
}
}
(i, j) to (j, n-1-i). That
mapping equals a transpose ((i,j)→(j,i)) followed by a
horizontal mirror ((j,i)→(j,n-1-i)) — both trivially in-place.Instinct A 2D grid you must walk in a fixed geometric order (spiral, diagonal, layer, rotate) or update in place - think boundaries, direction vectors and index maths.
Memory hack Walk the walls, then shrink the room - four fences (top, bottom, left, right) close in after every lap.
Open the Matrix Traversal guide →Approaches
Brute ForceAuxiliary Matrix
O(n2)Space O(n2)Apply the rotation formula directly: write matrix[i][j] into
rotated[j][n-1-i] of a fresh grid, then copy the grid back. A second
buffer means no value is overwritten before it is read.
public void rotate(int[][] matrix) { int n = matrix.length; int[][] rotated = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rotated[j][n - 1 - i] = matrix[i][j]; } } for (int i = 0; i < n; i++) { System.arraycopy(rotated[i], 0, matrix[i], 0, n); } }
Direct and bug-free, but the O(n2) buffer violates the in-place rule.
BetterTranspose, then Reverse Rows
O(n2)Space O(1)Swap across the main diagonal (only j > i, or every pair is
swapped twice), then reverse each row. Composing the two maps gives exactly
(i,j)→(j,n-1-i), using only swaps.
public void rotate(int[][] matrix) { int n = matrix.length; for (int i = 0; i < n; i++) { // transpose 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) { // reverse each row for (int l = 0, r = n - 1; l < r; l++, r--) { int t = row[l]; row[l] = row[r]; row[r] = t; } } }
In-place and easy to remember, but every cell is touched in two separate passes.
OptimalLayer-by-Layer 4-Way Cycle
O(n2)Space O(1)Peel the grid into concentric rings. On each ring, the four cells
top → right → bottom → left form a 4-cycle under rotation,
so one temp variable rotates all four at once. Every cell is moved exactly once
in a single pass.
public void rotate(int[][] matrix) { int n = matrix.length; for (int layer = 0; layer < n / 2; layer++) { int first = layer, last = n - 1 - layer; for (int i = first; i < last; i++) { int off = i - first; int top = matrix[first][i]; matrix[first][i] = matrix[last - off][first]; // left -> top matrix[last - off][first] = matrix[last][last - off]; // bottom -> left matrix[last][last - off] = matrix[i][last]; // right -> bottom matrix[i][last] = top; // top -> right } } }
Same Big-O, but one pass and ~n2 writes; index math is easier to get wrong.
[[1,2],[3,4]]
must become [[3,1],[4,2]].At a glance
| Level | Approach | Time | Space |
|---|---|---|---|
| Brute Force | Auxiliary Matrix | O(n2) | O(n2) |
| Better | Transpose, then Reverse Rows | O(n2) | O(1) |
| Optimal | Layer-by-Layer 4-Way Cycle | O(n2) | O(1) |
- Transposing with
jfrom 0 — each pair swaps twice and nothing changes. - Reversing columns instead of rows — that rotates counter-clockwise.
- Off-by-one on ring bounds: inner loop runs
first..last-1, not tolast.
- Derive
(i,j)→(j,n-1-i)first; every approach falls out of it. - Mention the counter-clockwise variant: transpose, then reverse each column.
- Offer the ring version when asked to minimise writes or do it in one pass.