mycode

思路:这种方格图一定要预先设置定位的变量,例如最大的长、宽,变化中的长、宽,在while循环中也要不断判断是否满足break条件

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        m = len(matrix)
        if m == 0 : return []   # []
        n = len(matrix[0])
        if n == 0 : return []  #[[],[]]
        m = m - 1; n = n - 1
        r ,l = 0 ,0 
        res = []
        #当r == n的时候,也还是要循环
        while r <= n and l <= m:
            for i in range(r,n+1):
                res.append(matrix[l][i])
            l = l + 1
            if l > m : break
            for i in range(l,m+1):
                res.append(matrix[i][n])
            n = n -1 ; i = n
            if n < r : break
            while i >= r:
                res.append(matrix[m][i])
                i -= 1
            m = m -1 ; i = m
            if m < l : break
            while i >= l:
                res.append(matrix[i][r])
                i -= 1
            r += 1
        return res