Easy python solution. Use pop funtion clockwise until matrix is empty


  • 0
    P

    class Solution(object):


    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        ret=[]
        while True:
            if len(matrix)==0 or len(matrix[0])==0: return ret
            ret+=matrix.pop(0)
            if len(matrix)==0 or len(matrix[0])==0: return ret
            ret+=[matrix[i].pop(len(matrix[i])-1) for i in xrange(len(matrix))]
            if len(matrix)==0 or len(matrix[0])==0: return ret
            ret+=matrix.pop(len(matrix)-1)[::-1]
            if len(matrix)==0 or len(matrix[0])==0: return ret
            ret+=[matrix[i].pop(0) for i in xrange(len(matrix))][::-1]

Log in to reply
 

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