-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0054_Spiral_Matrix.py
More file actions
74 lines (63 loc) · 2.58 KB
/
Copy path0054_Spiral_Matrix.py
File metadata and controls
74 lines (63 loc) · 2.58 KB
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
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
'''solution 0'''
# if not matrix:
# return []
# ans = []
# n_row, n_col = len(matrix), len(matrix[0])
# i = j = 0
# oris = {(0, 1):(1, -1), (1, 0):(-1, -1), (0, -1):(-1, 1), (-1, 0):(1, 1)}
# while True:
# for ori, ij_rev in oris.items():
# if len(ans) == n_row * n_col:
# return ans
# while i >= 0 and i < n_row and j >= 0 and j < n_col and matrix[i][j] != None:
# ans.append(matrix[i][j])
# matrix[i][j] = None
# i, j = i + ori[0], j + ori[1]
# i, j = i + ij_rev[0], j + ij_rev[1]
'''solution 1: with auxiliary matrix O(n)'''
# directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
# width, height = len(matrix[0]), len(matrix)
# matrix_visited = [[False] * width for _ in range(height)]
# count = 0
# i = j = 0
# ans = []
# d_i = 0
# while count < width * height:
# ans.append(matrix[i][j])
# matrix_visited[i][j] = True
# count += 1
# if count == width * height:
# break
# i_tmp = i + directions[d_i][0]
# j_tmp = j + directions[d_i][1]
# while not (0 <= i_tmp < height and 0 <= j_tmp < width and not matrix_visited[i_tmp][j_tmp]):
# d_i = (d_i + 1) % len(directions)
# i_tmp = i + directions[d_i][0]
# j_tmp = j + directions[d_i][1]
# i, j = i_tmp, j_tmp
# return ans
'''solution 2: using boundaries without auxiliary matrix'''
top, bottom, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
ans = []
while top <= bottom and left <= right:
# row right-ward
for j in range(left, right + 1):
ans.append(matrix[top][j])
top += 1
# col down-ward
for i in range(top, bottom + 1):
ans.append(matrix[i][right])
right -= 1
if top <= bottom:
# row left-ward
for j in range(right, left - 1, -1):
ans.append(matrix[bottom][j])
bottom -= 1
if left <= right:
# col up-ward
for i in range(bottom, top - 1, -1):
ans.append(matrix[i][left])
left += 1
return ans