Simple Python solution by rotation.

  • 1
    class Solution:
        # @param matrix, a list of lists of integers
        # @return a list of lists of integers
        def rotate(self, matrix):
            if matrix == []:
                return []
            n = len(matrix[0])
            start = 0
            end = n-1
            while start < end:
                for i in range(start, end):
                    matrix[i][end], matrix[end][end-i+start], matrix[end-i+start][start], matrix[start][i] = matrix[start][i], matrix[i][end], matrix[end][end-i+start], matrix[end-i+start][start]
                start += 1
                end -= 1
            return matrix

Log in to reply

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