螺旋矩阵
给定一个m行n列的矩阵,按顺时针螺旋顺序返回矩阵中的所有元素。
例如:
1
逐层返回,外层到内层。每层分四个方向。
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix or not matrix[0]:
return list()
row = len(matrix)
col = len(matrix[0])
count = 0
a = []
row_start = 0
row_end = row-1
col_start = 0
col_end = col-1
i = row_start
j = col_start
while count < row*col:
a.append(matrix[i][j])
count += 1
if i == row_start and j < col_end:
j += 1
continue
if j == col_end and i < row_end:
i += 1
continue
if i == row_end and j > col_start:
j -= 1
continue
if j == col_start and i > row_start+1:
i -= 1
continue
if i == row_start+1 and j == col_start:
row_start += 1
col_start += 1
row_end -= 1
col_end -= 1
i = row_start
j = col_start
continue
return a