48. Rotate Image

  • Array
  • Math
  • Matrix
TIME
O(n²)
SPACE
O(1)
problem on leetcode ↗source on github ↗

Intuition

A clockwise rotation moves each value from (row, col) to (col, n - 1 - row). Instead of creating another matrix, we can get the same final layout with two in-place operations:

  1. Transpose the matrix across the main diagonal.
  2. Reverse each row.

After transposing, rows become columns. Reversing each row then puts those columns in clockwise order.

Approach

  1. Let n be the matrix size.
  2. Transpose the matrix:
    • Swap matrix[row][col] with matrix[col][row] for every col > row.
  3. Reverse every row in-place.

Complexity

  • Time: O(n²), each matrix cell is touched a constant number of times
  • Space: O(1), only temporary variables are used

Edge Cases

  • n = 1 → the matrix remains unchanged
  • Negative values → values are only moved, not compared or computed
  • Even and odd n → transpose and row reversal handle both

Code

from typing import List


class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        48. Rotate Image
        Time: O(n²)
        Space: O(1)
        """
        n = len(matrix)

        for row in range(n):
            for col in range(row + 1, n):
                matrix[row][col], matrix[col][row] = matrix[col][row], matrix[row][col]

        for row in matrix:
            row.reverse()