Rotate image

August 7, 2022

math-and-geometry

Problem URL: Rotate image

We will save the topleft to a temporary variable and then move the bottom left to top left, then move the bottom right to bottom left and move the top right to bottom right and then finally move the temporary top left to top right. We will repeat the whole process until left and right meet together.

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        l, r = 0, len(matrix)-1
        while l < r:
            for i in range(r-l):
                top, bottom = l, r

                # save the topleft
                topLeft = matrix[top][l+i]

                # move bottom left into top left
                matrix[top][l + i] = matrix[bottom - i][l]

                # move bottom right into bottom left
                matrix[bottom - i][l] = matrix[bottom][r - i]

                # move top right into bottom right
                matrix[bottom][r - i] = matrix[top + i][r]

                # move top left into top right
                matrix[top + i][r] = topLeft

            r -= 1
            l += 1

Time Complexity: O(n)
Space Complexity: O(1)