48ms Python Solution


  • 2
    E

    Strip down the matrix layer by layer:

    class Solution:
    # @param {integer[][]} matrix
    # @return {integer[]}
    def spiralOrder(self, matrix):
        
        result = []
        while matrix:
            try:
                result += matrix.pop(0)
            except:
                break
            try:
                for row in matrix:
                    result += [row.pop(-1)]
            except:
                break
            try:
                result += matrix.pop(-1)[-1::-1]
            except:
                break
            try:
                for row in matrix[-1::-1]:
                    result += [row.pop(0)]
            except:
                break
        return result

Log in to reply
 

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