Python, beats 95%


  • 0
    M

    '''

    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        spiral = []
        while matrix and matrix[0]:
            spiral += matrix[0]
            matrix = matrix[1:]
            if not matrix:
                break
            spiral += [m[-1] for m in matrix]
            matrix = [m[:-1] for m in matrix]
            if not matrix[0]:
                break
            spiral += matrix[-1][::-1]
            matrix = matrix[:-1]
            if not matrix:
                break
            spiral += [m[0] for m in matrix][::-1]
            matrix = [m[1:] for m in matrix]
        return spiral

Log in to reply
 

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