← DSA Atlas
Dedicated problem page · #48

Rotate Image

MediumMatrix and SimulationTranspose then reverse rowsIn-place matrix manipulation
Solve on LeetCode ↗
48
MediumMatrix and SimulationIn-place matrix manipulationTranspose then reverse rows

Rotate Image

Given an n x n matrix, rotate it 90 degrees clockwise in place, modifying the input matrix directly without allocating another 2D matrix.

Open official problem prompt ↗
In plain English

Turn the entire square grid a quarter turn clockwise using only swaps inside the given matrix.

Picture it like this

Like rotating a physical photo on a table: first flip it along its diagonal crease, then flip each horizontal strip end-for-end, and it ends up turned 90 degrees.

Example
Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[[7,4,1],[8,5,2],[9,6,3]]
Why
The first column bottom-to-top (7,4,1) becomes the first row, which is exactly a 90-degree clockwise rotation.
Constraints
n == matrix.length == matrix[i].length1 <= n <= 20-1000 <= matrix[i][j] <= 1000
Pattern lesson

See the pattern, then code

Transpose then reverse rows
Recognition clue

A square matrix that must be rotated in place is the classic signal to decompose the rotation into a transpose followed by a per-row reversal.

Matrix and Simulation

Rotations, boundary walks, direction changes, or careful in-place state updates.. A 90-degree clockwise rotation equals reflecting across the main diagonal (transpose) and then reflecting across the vertical center (reverse each row). Both reflections are cheap in-place swaps.

New words, made simpleKnow these before the algorithm
Transpose
Swapping rows and columns so element (i,j) moves to (j,i).
In place
Modifying the input structure without a second matrix of the same size.
Main diagonal
The cells (0,0),(1,1),... that stay fixed during a transpose.
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
Copy into a new rotated matrix

Correct but violates the in-place requirement and doubles memory.

Allocate result[j][n-1-i] = matrix[i][j].

Time O(n^2)Space O(n^2)
Four-way cycle swap

In place and valid, but index bookkeeping is error prone.

Rotate four corner cells at a time in concentric rings.

Time O(n^2)Space O(1)
The rule we keep true

Invariant

After the transpose loop, matrix[i][j] holds the original matrix[j][i]; after reversing each row, matrix[i][j] holds the original matrix[n-1-j][i], which is the clockwise-rotated value.

Why this is correct

Reasoning

Clockwise rotation maps original position (r,c) to (c, n-1-r). Transpose sends (r,c) to (c,r); reversing row c sends (c,r) to (c, n-1-r). The composition is exactly the rotation map, so every element lands where it should.

The algorithm in three movesSay these aloud before coding
1Transpose the matrix by swapping matrix[i][j] with matrix[j][i] for j > i

transpose: [[1,4,7],[2,5,8],[3,6,9]]

2Reverse each row in place

reverse row 0: [7,4,1]

3The matrix now holds the clockwise rotation

reverse all: [[7,4,1],[8,5,2],[9,6,3]]

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
10
21
32
43
54
65
76
87
98
1 · Readpairs (0,1),(0,2),(1,2)
2 · AskSwap across diagonal?
3 · Update statematrix becomes [[1,4,7],[2,5,8],[3,6,9]]
4 · Resultcolumns are now rows
Key takeaway

The diagonal 1,5,9 stays fixed under transpose; then each row is flipped left-right.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 4-6Transpose

    Only j > i is visited so each off-diagonal pair is swapped exactly once and the diagonal is left alone.

  2. 2
    Lines 7-8Reverse each row

    In-place list reversal completes the rotation without extra storage.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • 1x1 matrix (unchanged)
  • 2x2 matrix
  • Matrices with negative or duplicate values
!

Common beginner mistakes

  • Looping j over the full range instead of j > i, which swaps every pair twice and undoes the transpose
  • Reversing columns instead of rows, which produces a counter-clockwise rotation
  • Allocating a new matrix and returning it instead of mutating in place (the function must return None)
Check your understanding

How would you rotate counter-clockwise instead?