Rotate 90°
To rotate a matrix 90° clockwise in-place: first transpose it (swap rows and columns: matrix[i][j] ↔ matrix[j][i]), then reverse each row. Two simple operations that combine elegantly.
How It Works
A 90-degree clockwise rotation decomposes into two elementary in-place operations: transpose the matrix (swap matrix[i][j] with matrix[j][i] across the main diagonal), then reverse each row. Transposing flips the matrix over its diagonal, and the row reversal converts that flip into the desired rotation. Both steps are simple double loops with no extra buffer.
Compared with the brute-force approach of copying into a second n x n matrix, this runs in the same O(n^2) time but O(1) space, which is exactly what problems demanding in-place rotation test. Counterclockwise rotation is the mirror recipe — transpose then reverse each column, or reverse rows first then transpose. An alternative is the four-way cyclic swap that moves elements in groups of four, but transpose-plus-reverse is far easier to write correctly under pressure.
Step-by-Step Visualization
Code
static void rotate(int[][] matrix) {
int n = matrix.length;
// Step 1: Transpose
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
int tmp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = tmp;
}
// Step 2: Reverse each row
for (int[] row : matrix) {
int l = 0, r = n - 1;
while (l < r) { int tmp = row[l]; row[l] = row[r]; row[r] = tmp; l++; r--; }
}
}
// [[1,2,3],[4,5,6],[7,8,9]] → [[7,4,1],[8,5,2],[9,6,3]]Tips & Gotchas
Practice Problems
- 1Rotate Image
- 2Transpose Matrix
- 3Determine Whether Matrix Can Be Obtained By Rotation
About the Traversal Patterns Pattern
Navigate a 2D grid in non-standard orders. The key is maintaining boundaries or using mathematical relationships between coordinates to determine the traversal path.
For traversal: use direction arrays dx=[0,0,1,-1], dy=[1,-1,0,0]. For sorted matrix search, start from top-right corner. For grid DP, fill row by row — current cell depends on top and left.
Common Matrix Interview Problems
- Spiral Matrix
- Rotate Image
- Search a 2D Matrix
- Number of Islands
- Maximal Square
- Set Matrix Zeroes
- Word Search
Frequently Asked Questions
Why does transpose followed by row reversal equal a clockwise rotation?
Transposing maps element (i, j) to (j, i), and reversing row j then maps it to (j, n-1-i). A 90-degree clockwise rotation sends (i, j) to exactly (j, n-1-i), so the composition of the two simple steps reproduces the rotation precisely.
What changes for a counterclockwise rotation?
Swap the order or the axis: either transpose and then reverse each column, or reverse each row first and then transpose. Both compositions map (i, j) to (n-1-j, i), which is the counterclockwise formula.
Does this work for non-square matrices?
Not in place — rotating an m x n matrix produces an n x m matrix, so the dimensions change and you need a new output array. The in-place transpose-and-reverse trick only applies when the matrix is square.