题目链接:https://leetcode.com/problems/spiral-matrix/
代码
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
return matrix and [*matrix.pop(0)] + self.spiralOrder([*zip(*matrix)][::-1])
思路
被别人的思路秒杀,甚是欣赏
参考链接:https://leetcode.com/problems/spiral-matrix/discuss/20571/1-liner-in-Python-%2B-Ruby
代码二
class Solution:
def spiralOrder(self, matrix):
if not matrix:return matrix
row,col=len(matrix)-1,len(matrix[0])-1
i,j=0,0
result=[]
while i <= row and j<= col :
result+=matrix[i][j:col+1]
i+=1
if i > row : break
for k in range(i,row+1):
result.append(matrix[k][col])
col-=1
if j > col : break
for k in range(col,j-1,-1):
result.append(matrix[row][k])
row-=1
for k in range(row,i-1,-1):
result.append(matrix[k][j])
j+=1
return result
思路说明:这个相对来说比较普通的剪枝思维