Rotate a Square Matrix 90 Degrees Clockwise

Solve this Problem
Medium20–25 min
Topics
Companies
Practice:GFG ↗
Given a square matrix, rotate it 90 degrees clockwise, in place: the element at matrix[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:

Input:matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output:[[7,4,1],[8,5,2],[9,6,3]]
Explanation:Each column of the original, read bottom-to-top, becomes a row of the result.

Test Case 2:

Input:matrix = [[1,2],[3,4]]
Output:[[3,1],[4,2]]
Explanation:The top-left corner (1) rotates to the top-right; the bottom-left corner (3) rotates to the top-left.

Test Case 3:

Input:matrix = [[5]]
Output:[[5]]
Explanation:A single cell has nowhere to rotate to.

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.

🧪Try your own test case
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}
24
1
2
3
4
5
6
7
8
9
Variables
n3
INITIALIZE

Rotate in two in-place phases: first transpose the matrix (swap across the main diagonal), then reverse every row. No extra matrix needed.

Step 1 / 11

Approach & Solutions

Brute Force — Extra Matrix

Brute

Allocate 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.

TimeO(n²)
SpaceO(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

Optimal

Do 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.

TimeO(n²)
SpaceO(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}

Related Problems