LC 0048 [M] Rotate Image - ALawliet/algorithms GitHub Wiki

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        l = 0
        r = len(matrix) - 1
        
        while l < r:
            for i in range(r - l): # i is the layer
                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 bottomr ight
                matrix[bottom][r - i] = matrix[top + i][r]
                
                # move top left into top right
                matrix[top + i][r] = topLeft
            r -= 1
            l += 1
class Solution:
    def rotate(self, A):
        n = len(A)
        mid = n // 2
        for i in range(mid):
            for j in range(n - mid):
                A[i][j], A[~j][i], A[~i][~j], A[j][~i] = \
                A[~j][i], A[~i][~j], A[j][~i], A[i][j] # like above but reverse