Rotate a Square Matrix 90 Degrees Clockwise
Solve this Problemmatrix[i][j] moves to result[j][n-1-i].
Building a brand new matrix and placing each element at its rotated destination works, but it costs a full second matrix's worth of memory. A 90° clockwise rotation can be decomposed into two simpler, in-place operations: a transposeTransposeFlipping a matrix across its main diagonal — matrix[i][j] and matrix[j][i] swap places. (flip across the main diagonal), followed by reversing every row (a horizontal flip). Composing those two flips is exactly the same as rotating 90° clockwise — no extra matrix required.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ n ≤ 20 - ◆
-1000 ≤ matrix[i][j] ≤ 1000 - ◆
matrix is a square matrix (n × n)
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int[][] rotateMatrix(int[][] matrix) { |
| 3 | int n = matrix.length; |
| 4 | for (int i = 0; i < n; i++) { |
| 5 | for (int j = i + 1; j < n; j++) { |
| 6 | int temp = matrix[i][j]; |
| 7 | matrix[i][j] = matrix[j][i]; |
| 8 | matrix[j][i] = temp; |
| 9 | } |
| 10 | } |
| 11 | for (int i = 0; i < n; i++) { |
| 12 | int left = 0, right = n - 1; |
| 13 | while (left < right) { |
| 14 | int temp = matrix[i][left]; |
| 15 | matrix[i][left] = matrix[i][right]; |
| 16 | matrix[i][right] = temp; |
| 17 | left++; |
| 18 | right--; |
| 19 | } |
| 20 | } |
| 21 | return matrix; |
| 22 | } |
| 23 | } |
| 24 |
3Rotate in two in-place phases: first transpose the matrix (swap across the main diagonal), then reverse every row. No extra matrix needed.
Approach & Solutions
Brute Force — Extra Matrix
BruteAllocate a brand new n×n matrix, and for every source cell, compute its rotated destination directly with the formula matrix[i][j] → result[j][n-1-i], then place it there. Correct and simple, but it needs a second full matrix's worth of memory.
O(n²)O(n²)1class Solution {
2 public int[][] rotateMatrix(int[][] matrix) {
3 int n = matrix.length;
4 int[][] result = new int[n][n];
5 for (int i = 0; i < n; i++) {
6 for (int j = 0; j < n; j++) {
7 result[j][n - 1 - i] = matrix[i][j];
8 }
9 }
10 return result;
11 }
12}Optimal — Transpose, Then Reverse Each Row
OptimalDo it in-place with two simple passes instead. First transpose the matrix — swap matrix[i][j] with matrix[j][i] for every pair above the main diagonal, which flips the matrix across that diagonal. Then reverse every row — a horizontal flip. A diagonal flip followed by a horizontal flip composes into exactly a 90° clockwise rotation, with no extra matrix required.
O(n²)O(1)1class Solution {
2 public int[][] rotateMatrix(int[][] matrix) {
3 int n = matrix.length;
4 for (int i = 0; i < n; i++) {
5 for (int j = i + 1; j < n; j++) {
6 int temp = matrix[i][j];
7 matrix[i][j] = matrix[j][i];
8 matrix[j][i] = temp;
9 }
10 }
11 for (int i = 0; i < n; i++) {
12 int left = 0, right = n - 1;
13 while (left < right) {
14 int temp = matrix[i][left];
15 matrix[i][left] = matrix[i][right];
16 matrix[i][right] = temp;
17 left++;
18 right--;
19 }
20 }
21 return matrix;
22 }
23}