题目描述:
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
测试用例:
代码如下:
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
i = 0
j = 0
if not matrix or not matrix[0]:
return []
m = len(matrix)
n = len(matrix[0])
a = 0
b = m-1
c = n-1
d = 0
z = []
if b == 0:
return matrix[0]
elif c == 0:
for each in range(b+1):
z.append(matrix[each][0])
else:
z.append(matrix[a][j])
d += 1
while a <= m//2 and d < m*n:
while i == a and j < c:
z.append(matrix[a][j+1])
d += 1
j += 1
if j == c:
while i < b:
z.append(matrix[i+1][c])
d += 1
i += 1
while i == b and j > a and a < m//2:
z.append(matrix[b][j-1])
d += 1
j -= 1
if j == a:
while i > a+1:
z.append(matrix[i-1][j])
d += 1
i -= 1
a += 1
b -= 1
c -= 1
return z