class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
while matrix:
res += matrix.pop(0)
matrix = list(map(list, zip(*matrix)))[::-1]
return res
import numpy as np
class Solution(object):
res = list()
def spiralOrder(self, matrix):
Solution.res = list()
self.helper(matrix)
return Solution.res
def helper(self, matrix):
if matrix is None or len(matrix) == 0 or len(matrix[0]) == 0:
return Solution.res
row = len(matrix)
col = len(matrix[0])
if row == 1:
# 只剩一行 橫向打印
for i in range(col):
Solution.res.append(matrix[0][i])
return Solution.res
if col == 1:
# 只剩一列 豎向打印
for i in range(row):
Solution.res.append(matrix[i][col - 1])
return Solution.res
# 從左到右
for i in range(col):
Solution.res.append(matrix[0][i])
# 從上到下
for i in range(1, row):
Solution.res.append(matrix[i][col - 1])
# 從右到左
for j in range(col - 2, -1, -1):
Solution.res.append(matrix[row - 1][j])
# 從下到上
for j in range(row - 2, 0, -1):
Solution.res.append(matrix[j][0])
matrix = np.array(matrix)
new_matrix = matrix[1: len(matrix) - 1, 1: len(matrix[0]) - 1]
self.helper(new_matrix)