Spiral Marix ,in python


  • 0
    M

    ’‘’
    class Solution(object):
    def spiralOrder(self, matrix):
    """
    :type matrix: List[List[int]]
    :rtype: List[int]
    """

        def four_side(row,col,x):
            for i in range(x,col):
                lis.append(matrix[x][i])
            for i in range(x+1,row):
                lis.append(matrix[i][col-1])
            for i in range(x,col-1)[::-1]:
                lis.append(matrix[row-1][i])
            for i in range(x+1,row-1)[::-1]:
                lis.append(matrix[i][x])
        if matrix==[]:
            return []
        else:
            x=0    
            m=row=len(matrix)
            n=col=len(matrix[0])
            mn=m*n
            lis=[]
            while len(lis)<mn:
                if 2*x==row+col:
                    lis.append(matrix[x][x])
                else:
                    four_side(row,col,x)
                    row-=1
                    col-=1
                    x+=1
            return lis[0:mn]       ‘’‘

Log in to reply
 

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