Rotate an n×n matrix 90° clockwise in-place. Step 1: transpose (swap across diagonal). Step 2: reverse each row. O(n²) time, O(1) space.
TimeO(n²)visit each cell
SpaceO(1)in-place swaps
Step: 0/9
Swapping
Completed
Default
Ready
Press Play. Transpose the matrix (swap across main diagonal), then reverse each row to achieve 90° clockwise rotation.
TimeO(n²)four-way swap per cell
SpaceO(1)one temp variable
Layer-by-layer: process each ring from outside in. For each position, rotate four elements in a cycle: top→right→bottom→left→top. Same O(n²) time but conceptually different.
✎ Whiteboard
3
⌨ Type It
Practice until you don't need to look. Use the guide comments below as scaffolding. The green highlights are the nuances to burn into memory.
═══ ROTATE IMAGE — TRANSPOSE + REVERSE ═══
PATTERN ▸ Transpose then reverse rows O(n²) · O(1)