目录
题目
模拟
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
m, n = len(matrix), len(matrix[0])
res = []
top, bottom = 0, m - 1
left, right = 0, n - 1
while top <= bottom and left <= right:
# 遍历上边界
for i in range(left, right + 1):
res.append(matrix[top][i])
top += 1
# 遍历右边界
for i in range(top, bottom + 1):
res.append(matrix[i][right])
right -= 1
# 遍历下边界
if top <= bottom:
for i in range(right, left - 1, -1):
res.append(matrix[bottom][i])
bottom -= 1
# 遍历左边界
if left <= right:
for i in range(bottom, top - 1, -1):
res.append(matrix[i][left])
left += 1
return res
标签:matrix,螺旋,int,54,top,矩阵,len,List
From: https://www.cnblogs.com/lushuang55/p/18107773