题目描述
方法:
动态更新遍历边界
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix:
return []
left, right, top, bottom = 0, len(matrix[0])-1, 0, len(matrix)-1
res = []
i = 0
while True:
for j in range(left,right+1):
res.append(matrix[i][j])
top += 1
if top > bottom:
break
for i in range(top,bottom+1):
res.append(matrix[i][j])
right -= 1
if left > right:
break
for j in range(right, left-1, -1):
res.append(matrix[i][j])
bottom -= 1
if top > bottom:
break
for i in range(bottom, top-1,-1):
res.append(matrix[i][j])
left += 1
if left > right:
break
return res