Short, in-place python solution

  • 1
    class Solution:
        # @param matrix, a list of lists of integers
        # @return a list of lists of integers
        def rotate(self, matrix):
            n = len(matrix)
            for i in range(n):
                for j in range(i, n):
                    tmp = matrix[i][j]
                    matrix[i][j] = matrix[j][i]
                    matrix[j][i] = tmp
            for i in range(n):
                matrix[i] = matrix[i][::-1]
            return matrix

  • 0

    It shakes up each cell twice.

    That said, a very clever approach. It is much cleaner than implementing the 90 degree rotation.

    Thumbs up!

Log in to reply

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