medium

Rotate Image

Rotate a square matrix 90 degrees in place.

1. Define the problem

Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees clockwise , done in place . You have to rotate the image without allocating another 2D matrix for the rotation. First transpose the matrix (flip it over its main diagonal), then reverse every row — the combination produces a clean 90-degree clockwise turn.

Constraints

  • n == matrix.length == matrixi.length
  • 1 ≤ n ≤ 20
  • -1000 ≤ matrixi[j] ≤ 1000

Example

Inputmatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output[[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Explanation Transposing swaps cells across the diagonal, then reversing each row turns the transposed matrix into the clockwise-rotated one.

2. Know the words first

In plain terms

Transpose
Swapping each cell (row, col) with cell (col, row), so rows become columns and columns become rows.
3. Visualize the solution

Transpose across the diagonal, then reverse every row

Transpose across the diagonal, then reverse every row
Statustranspose

Swap (0,1) and (1,0): 2 and 4 trade places.

What happens in this step

row=0, col=1
swap matrix[0][1] and matrix[1][0]

2 and 4 swap places as part of the transpose.
Step 1 of 6

Steps to visualize

  1. Walk through every cell above the main diagonal and swap it with its mirror cell below the diagonal.
  2. This flips the matrix over its diagonal, turning rows into columns.
  3. Once the transpose is complete, reverse each row from left to right.
  4. The transposed-then-reversed matrix is exactly the original rotated 90 degrees clockwise.
4. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Transpose across the diagonal, then reverse every row
Statustranspose

Swap (0,1) and (1,0): 2 and 4 trade places.

What happens in this step

row=0, col=1
swap matrix[0][1] and matrix[1][0]

2 and 4 swap places as part of the transpose.
Step 1 of 6
5. Solution

Solution

solution.tsTypeScript
function rotate(matrix) {
  const n = matrix.length;

  for (let row = 0; row < n; row++) {
    for (let col = row + 1; col < n; col++) {
      const temp = matrix[row][col];
      matrix[row][col] = matrix[col][row];
      matrix[col][row] = temp;
    }
  }

  for (let row = 0; row < n; row++) {
    matrix[row].reverse();
  }

  return matrix;
}
Time
O(n^2)
Space
O(1)
6. Test cases

Test cases

InputExpectedCovers
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]][[7, 4, 1], [8, 5, 2], [9, 6, 3]]example from the docstring
matrix = [[1]][[1]]smallest valid input: a single cell
matrix = [[1, 2], [3, 4]][[3, 1], [4, 2]]the smallest matrix that actually rotates visibly
matrix = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]][[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]a larger 4x4 matrix
matrix = [[-1, -2], [-3, -4]][[-3, -1], [-4, -2]]negative values throughout the matrix
matrix = [[1, 1], [1, 1]][[1, 1], [1, 1]]every cell holding the same value