28ms python solution


  • 0
    B
        def spiralOrder(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: List[int]
            """
            if not matrix:
                return []
            r=len(matrix)
            c=len(matrix[0])
            dict={'r':(1,0,'d'),'d':(0,-1,'l'),'l':(-1,0,'u'),'u':(0,1,'r')}
            spiral=[]
            current='r'
            ir,ic=0,1
            cr,cc=0,0
            while cr<=r-1 and cc<=c-1 and matrix[cr][cc]!=None:
                spiral.append(matrix[cr][cc])
                matrix[cr][cc]=None
                if cr+ir>r-1 or cc+ic>c-1 or matrix[cr+ir][cc+ic]==None:
                    ir=dict[current][0]
                    ic=dict[current][1]
                    current=dict[current][2]
                cr,cc=cr+ir,cc+ic
                    
            return spiral

Log in to reply
 

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