给你一个 m
行 n
列的矩阵 matrix
,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
例:
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2,3,6,9,8,7,4,5]
解析:主要是运用zip()函数(zip()函数的用法可见:Python zip() 函数 | 菜鸟教程)
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
ans = []
while matrix:
ans.extend(matrix[0])
matrix = list(zip(*matrix[1:]))[::-1] # "*"相当于用来解压缩,把最外面的一层中括号去掉,相当于给zip多个参数进行打包而不是一个
return ans