[LeetCode]54 螺旋矩阵

题目描述

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例1:

输入:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]

示例2:

输入:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == [] : return []
res = []
maxUp = maxLeft = 0
maxDown = len(matrix) - 1
maxRight = len(matrix[0]) - 1
direction = 0 # 0 go right, 1 go down, 2 go left, 3 up
while True:
if direction == 0: #go right
for i in range(maxLeft, maxRight+1):
res.append(matrix[maxUp][i])
maxUp += 1
elif direction == 1: # go down
for i in range(maxUp, maxDown+1):
res.append(matrix[i][maxRight])
maxRight -= 1
elif direction == 2: # go left
for i in reversed(range(maxLeft, maxRight+1)):
res.append(matrix[maxDown][i])
maxDown -= 1
else: #go up
for i in reversed(range(maxUp, maxDown+1)):
res.append(matrix[i][maxLeft])
maxLeft +=1
if maxUp > maxDown or maxLeft > maxRight:
return res
direction = (direction + 1 ) % 4