Rotate Image
Medium
Topics
Given an n x n matrix, rotate it 90 degrees clockwise. LeetCode mutates in place and returns void; here return the rotated matrix.
Example 1
top rowwhere it lands
Rotating 90° clockwise sends the top row to the right column, the right column to the bottom row, and so on — one four-way swap per ring. It must happen in place, with no second matrix.
2D grid — one row per line.
Input: grid = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2
top rowwhere it lands
Input: grid = [[1,2],[3,4]] Output: [[3,1],[4,2]]
Constraints
- 1 <= n <= 20
- -1000 <= matrix[i][j] <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
A 90-degree clockwise rotation is the composition of two simple in-place operations: transpose the matrix (swap across the main diagonal), then reverse each row.
Approach
- Transpose: swap matrix[i][j] with matrix[j][i] for i < j.
- Reverse each row.
- The result is the clockwise rotation.
Dry Run
grid 1 2 3
4 5 6
7 8 9
A 90-degree clockwise rotation factors into two cheap in-place passes.
transpose, swapping only (0,1)-(1,0), (0,2)-(2,0), (1,2)-(2,1):
1 4 7
2 5 8
3 6 9
then reverse each row:
7 4 1
8 5 2
9 6 3
answer 7 4 1 | 8 5 2 | 9 6 3
The inner loop starts at `j = i + 1`, and that is the only subtle line. Each pair
of mirrored cells must be swapped ONCE; visiting the full square swaps every pair
twice, which puts the matrix back as it was, so the row reversal alone remains and
the result is a horizontal mirror: 3 2 1 | 6 5 4 | 9 8 7.
Starting at i + 1 rather than i also skips the diagonal, where a swap would be a
no-op anyway.
Transpose-then-reverse-rows gives clockwise. Reversing the rows FIRST and then
transposing gives anticlockwise -- same two passes, opposite order, which is worth
knowing rather than deriving under pressure.Complexity
- Time — O(n^2): touch each cell a constant number of times.
- Space — O(1): in-place swaps.
Common Pitfalls
- Transpose only the upper triangle (j > i), or you swap everything back.
Full Solution — all languages
class Solution:
def rotate(self, grid):
n=len(grid)
for i in range(n):
for j in range(i+1,n):
grid[i][j],grid[j][i]=grid[j][i],grid[i][j]
for row in grid:
row.reverse()
return gridimport java.util.*;
class Solution {
public int[][] rotate(int[][] grid) {
int n=grid.length;
for(int i=0;i<n;i++)for(int j=i+1;j<n;j++) {
int t=grid[i][j];
grid[i][j]=grid[j][i];
grid[j][i]=t;
}
for(int i=0;i<n;i++) {
for(int l=0,r=n-1;l<r;l++,r--) {
int t=grid[i][l];
grid[i][l]=grid[i][r];
grid[i][r]=t;
}
}
return grid;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<vector<int>> rotate(vector<vector<int>>& grid) {
int n=grid.size();
for(int i=0;i<n;i++)for(int j=i+1;j<n;j++)swap(grid[i][j],grid[j][i]);
for(auto& row:grid)reverse(row.begin(),row.end());
return grid;
}
};/**
* @param {number[][]} grid
* @return {number[][]}
*/ var rotate = function(grid) {
const n=grid.length;
for(let i=0;i<n;i++)for(let j=i+1;j<n;j++) {
const t=grid[i][j];
grid[i][j]=grid[j][i];
grid[j][i]=t;
}
for(const row of grid)row.reverse();
return grid;
};