LeetCode_Spiral Matrix

Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
(螺旋式遍历矩阵)

Example:



1. 方向模拟

事先定义四个方向,按照题中的螺旋式的方向在遍历矩阵,过程中遇到边界时不断的修改方向并修改新的边界。具体实现过程如下:

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
33
34
35
36
37
38
39
40
41
42
43
class Solution:
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix:
return []

m = len(matrix)
n = len(matrix[0])

direction = {
'right': [0, 1],
'down': [1, 0],
'left': [0, -1],
'up': [-1, 0]
}

answer = []
i, j = 0, 0
direct = 'right'
left_border, right_border, up_border, down_border = 0, n-1, 0, m-1

while len(answer) < m * n:
answer.append(matrix[i][j])

if i==up_border and j==right_border and direct=='right':
direct = 'down'
up_border += 1
elif i==down_border and j==right_border and direct=='down':
direct = 'left'
right_border -= 1
elif i==down_border and j==left_border and direct=='left':
direct = 'up'
down_border -= 1
elif i==up_border and j==left_border and direct=='up':
direct = 'right'
left_border += 1
i += direction[direct][0]
j += direction[direct][1]

return answer

2. zip函数

在看别人的简单解法中发现用zip不断地旋转矩阵,每次都获得矩阵的第一行就是螺旋式遍历的结果。具体实现方法如下:

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
answer = []
while matrix:
answer += matrix.pop(0)
matrix = [*zip(*matrix)][::-1]
return answer