leetcode 54 旋转矩阵I

题目:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:

Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
题解:
return matrix and list(matrix.pop(0))+self.spiralOrder(zip(*matrix)[::-1])
分解过程解析一下
spiral_order([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

= [1, 2, 3] + spiral_order([[6, 9],
[5, 8],
[4, 7]])

= [1, 2, 3] + [6, 9] + spiral_order([[8, 7],
[5, 4]])

= [1, 2, 3] + [6, 9] + [8, 7] + spiral_order([[4],
[5]])

= [1, 2, 3] + [6, 9] + [8, 7] + [4] + spiral_order([[5]])

= [1, 2, 3] + [6, 9] + [8, 7] + [4] + [5] + spiral_order([])

= [1, 2, 3] + [6, 9] + [8, 7] + [4] + [5] + []

= [1, 2, 3, 6, 9, 8, 7, 4, 5]

猜你喜欢

转载自blog.csdn.net/weixin_44482648/article/details/86517762