-
Notifications
You must be signed in to change notification settings - Fork 0
/
0054SpiralMatrix.py
101 lines (95 loc) · 2.95 KB
/
0054SpiralMatrix.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Given a matrix of m x n elements (m rows, n columns),
return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Corner:
1. row, just return
2. col, transpose and return
Note: 0 > None, but bool(0) is False
"""
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
# brute force
# traversal along the encounter and visit node
# mark visited nodes as None,
# flag direction, start from right, then dn, then left, then up, change direction when None or boundary encountered
if len(matrix) ==0:
return matrix
elif len(matrix)==1:
return matrix[0]
elif len(matrix[0]) == 1:
return [matrix[i][0] for i in range(len(matrix))]
m = len(matrix)
n = len(matrix[0])
res = [matrix[0][0]]
dire = 0
cur = [0, 0]
matrix[0][0]=None
while True:
while dire == 0:
cur[1] += 1
if cur[1] < n and matrix[cur[0]][cur[1]]!= None:
res.append(matrix[cur[0]][cur[1]])
matrix[cur[0]][cur[1]]=None
else:
cur[1] -= 1
dire = (dire+1) % 4
while dire == 1:
cur[0] += 1
if cur[0] < m and matrix[cur[0]][cur[1]] != None:
res.append(matrix[cur[0]][cur[1]])
matrix[cur[0]][cur[1]]=None
else:
cur[0] -= 1
dire = (dire+1) % 4
while dire == 2:
cur[1] -= 1
if cur[1] >= 0 and matrix[cur[0]][cur[1]]!= None :
res.append(matrix[cur[0]][cur[1]])
matrix[cur[0]][cur[1]]=None
else:
cur[1] += 1
dire = (dire+1) % 4
while dire == 3:
cur[0] -= 1
if cur[0] >= 0 and matrix[cur[0]][cur[1]]!= None :
res.append(matrix[cur[0]][cur[1]])
matrix[cur[0]][cur[1]]=None
else:
cur[0] += 1
dire = (dire+1) % 4
# finished a circle, check next pos
if cur[1]+1 >= n or matrix[cur[0]][cur[1]+1] == None:
break
return res
# Do it layer by layer could be faster
if __name__ == '__main__':
sl = Solution()
nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
num1 = [[1,2,3,4]]
num2 = [[2,5],[8,4],[0,-1]]
num3 = [[1],[2],[3]]
num4=[]
tmp = sl.spiralOrder(nums)
print(sl.spiralOrder(num1))
print(sl.spiralOrder(num2))
print(sl.spiralOrder(num3))
print(sl.spiralOrder(num4))
assert tmp == [1,2,3,6,9,8,7,4,5]
print(tmp)