Python Solution Using Mathematics

  • 0

    A 90° clockwise rotation is equivalent to flipping over the horizontal axis, and flipping it across the diagonal axis \. Understanding this mathematically, we implement in code.

    def rotate(self, matrix):
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        # 90 deg clockwise rotation = flip over horizontal + flip over top-left / bottom-right axis
        for row in range(len(matrix) // 2):
            matrix[row], matrix[len(matrix) - (row + 1)] = matrix[len(matrix) - (row + 1)], matrix[row]
        for row in range(len(matrix)):
            for col in range(row, len(matrix[row])):
                matrix[row][col], matrix[col][row] = matrix[col][row], matrix[row][col]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.