class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix: return [];
m=len(matrix)
n=len(matrix[0])
dx = [0,1,0,-1];dy=[1,0,-1,0] #左下右上别搞错
x=0;y=0
d=0
res=[]
for i in range(m*n):
res.append(matrix[x][y])
matrix[x][y]='#'
a=x+dx[d];b= y+dy[d]
if not 0<=a<m or not 0<=b<n or matrix[a][b]=='#':
d=(d+1)%4
a=x+dx[d];b= y+dy[d]
x=a;y=b
return res
剑指 Offer 29. 顺时针打印矩阵
最新推荐文章于 2022-10-16 16:58:48 发布