一、leetcode地址
https://leetcode.com/problems/spiral-matrix/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
m = len(matrix)
n = len(matrix[0]) if len(matrix) != 0 else 0
if m*n == 0:
return []
imin = 0;imax = m-1;jmin = 0;jmax = n-1
i = 0;j = 0;direction = 0;no = 0
result = []
while no < m*n:
result.append(matrix[i][j])
if direction == 0:
if j == jmax:
direction = (direction+1)%4
imin += 1
i += 1
else:
j += 1
elif direction == 1:
if i == imax:
direction = (direction+1)%4
jmax -= 1
j -= 1
else:
i += 1
elif direction == 2:
if j == jmin:
direction = (direction+1)%4
imax -=1
i -= 1
else:
j -= 1
else:
if i == imin:
direction = (direction+1)%4
jmin += 1
j += 1
else:
i -= 1
no+= 1
print(i,j,imin,result)
return result