class Solution:
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if len(matrix)==0 or len(matrix[0])==0:
            return matrix
        l,r,u,d=0,len(matrix[0])-1,0,len(matrix)-1
        res=[]
        while True:
            #go right
            for col in range(l,r+1,1):
                res.append(matrix[u][col])
            u+=1#u一排全都输入了
            if u>d:
                break
            #go down
            for row in range(u,d+1,1):
                res.append(matrix[row][r])
            r-=1
            if r<l:
                break
            #go left
            for col in range(r,l-1,-1):
                res.append(matrix[d][col])
            d-=1
            if d<u:
                    break
            #go up
            for row in range(d,u-1,-1):
                res.append(matrix[row][l])
            l+=1
            if l>r:
                break
        return res
s=Solution()
print(s.spiralOrder([[1,2,3],[4,5,6],[7,8,9]]))