-
Notifications
You must be signed in to change notification settings - Fork 43
/
spiral-matrix-ii.py
217 lines (196 loc) · 6.81 KB
/
spiral-matrix-ii.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
59. Spiral Matrix II
Medium
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
Example 1:
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
Example 2:
Input: n = 1
Output: [[1]]
Constraints:
1 <= n <= 20
"""
# V0
class Solution:
def generateMatrix(self, n):
# Build n by n matrix
matrix = [[None] * n for row in range(n)]
min_row, max_row, min_column, max_column = 0, n - 1, 0, n - 1
spiral_element = 1
# while not go through all elements in matrix
while spiral_element <= n ** 2:
# right
for column in range(min_column, max_column + 1):
matrix[min_row][column] = spiral_element
spiral_element += 1
min_row += 1
# down
for row in range(min_row, max_row + 1):
matrix[row][max_column] = spiral_element
spiral_element += 1
max_column -= 1
# left
for column in range(max_column, min_column - 1 , -1):
matrix[max_row][column] = spiral_element
spiral_element += 1
max_row -= 1
# up
for row in range(max_row, min_row - 1, -1):
matrix[row][min_column] = spiral_element
spiral_element += 1
min_column += 1
return matrix
# V1
# https://leetcode.com/problems/spiral-matrix-ii/discuss/572973/easy-to-understand-python-solution-beats-93
# ALSO ref 054 spiral-matrix:
# https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/spiral-matrix.py
# IDEA : SPIRAL MATRIX
# NOTICE
# In [4]:
# ...: for i in range(3, -1 ,-1):
# ...: print (i)
# ...:
# 3
# 2
# 1
# 0
class Solution:
def generateMatrix(self, n):
# Build n by n matrix
matrix = [[None] * n for row in range(n)]
min_row, max_row, min_column, max_column = 0, n - 1, 0, n - 1
spiral_element = 1
# while not go through all elements in matrix
while spiral_element <= n ** 2:
# right
for column in range(min_column, max_column + 1):
matrix[min_row][column] = spiral_element
spiral_element += 1
min_row += 1
# down
for row in range(min_row, max_row + 1):
matrix[row][max_column] = spiral_element
spiral_element += 1
max_column -= 1
# left
for column in range(max_column, min_column - 1 , -1):
matrix[max_row][column] = spiral_element
spiral_element += 1
max_row -= 1
# up
for row in range(max_row, min_row - 1, -1):
matrix[row][min_column] = spiral_element
spiral_element += 1
min_column += 1
return matrix
### Test case
s = Solution()
s.generateMatrix(0) == []
assert s.generateMatrix(1) == [[1]]
assert s.generateMatrix(2) == [[1, 2], [4, 3]]
assert s.generateMatrix(3) == [[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]]
assert s.generateMatrix(4) == [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79541687
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
visited = [[0] * n for _ in range(n)]
matrix = [[0] * n for _ in range(n)]
self.row, self.col = 0, 0
self.curr = 1
def spiral():
move = False
while self.col < n and not visited[self.row][self.col]:
matrix[self.row][self.col] = self.curr
self.curr += 1
visited[self.row][self.col] = 1
self.col += 1
move = True
self.col -= 1
self.row += 1
while self.row < n and not visited[self.row][self.col]:
matrix[self.row][self.col] = self.curr
self.curr += 1
visited[self.row][self.col] = 1
self.row += 1
move = True
self.row -= 1
self.col -= 1
while self.col >= 0 and not visited[self.row][self.col]:
matrix[self.row][self.col] = self.curr
self.curr += 1
visited[self.row][self.col] = 1
self.col -= 1
move = True
self.col += 1
self.row -= 1
while self.row >= 0 and not visited[self.row][self.col]:
matrix[self.row][self.col] = self.curr
self.curr += 1
visited[self.row][self.col] = 1
self.row -= 1
move = True
self.row += 1
self.col += 1
if move:
spiral()
spiral()
return matrix
# V1'
class Solution:
# @return a list of lists of integer
def generateMatrix(self, n):
if n == 0: return []
matrix = [[0 for i in range(n)] for j in range(n)]
up = 0; down = len(matrix)-1
left = 0; right = len(matrix[0])-1
direct = 0; count = 0
while True:
if direct == 0:
for i in range(left, right+1):
count += 1; matrix[up][i] = count
up += 1
if direct == 1:
for i in range(up, down+1):
count += 1; matrix[i][right] = count
right -= 1
if direct == 2:
for i in range(right, left-1, -1):
count += 1; matrix[down][i] = count
down -= 1
if direct == 3:
for i in range(down, up-1, -1):
count += 1; matrix[i][left] = count
left += 1
if count == n*n: return matrix
direct = (direct+1) % 4
# V2
# Time: O(n^2)
# Space: O(1)
class Solution(object):
# @return a list of lists of integer
def generateMatrix(self, n):
matrix = [[0 for _ in range(n)] for _ in range(n)]
left, right, top, bottom, num = 0, n - 1, 0, n - 1, 1
while left <= right and top <= bottom:
for j in range(left, right + 1):
matrix[top][j] = num
num += 1
for i in range(top + 1, bottom):
matrix[i][right] = num
num += 1
for j in reversed(range(left, right + 1)):
if top < bottom:
matrix[bottom][j] = num
num += 1
for i in reversed(range(top + 1, bottom)):
if left < right:
matrix[i][left] = num
num += 1
left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1
return matrix