My Python Solution


  • 0
    class Solution(object):
        def spiralOrder(self, matrix):
            if not matrix:
                return []
            res = matrix[0]
            dirs = [(1,0),(0,1),(-1,0),(0,-1)]
            m, n = len(matrix), len(matrix[0])
            x,y,d,i = n-1,0,1,m*n-n
            while i > 0:
                for j in xrange(m-1):
                    x,y = x+dirs[d][0], y+dirs[d][1]
                    res.append(matrix[y][x])
                    i -= 1
                d = (d+1)%4
                m -= 1
                m,n = n,m
            return res

Log in to reply
 

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