Notes

Personal notes on various topics

View on GitHub

Intuition

To rotate a matrix 90 degrees clockwise in-place, we can use two simple operations: transpose the matrix and then reflect each row horizontally.

Approach

Complexity

Code

from typing import List

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        def transpose():
            for i in range(n):
                for j in range(i):
                    matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

        def reflect():
            for i in range(n):
                for j in range(n // 2):
                    matrix[i][j], matrix[i][-j - 1] = matrix[i][-j - 1], matrix[i][j]

        n = len(matrix)
        transpose()
        reflect()

Back to Problem Statement