-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleet_0059_spiral_matrix_II__OLD.py
41 lines (41 loc) · 1.08 KB
/
leet_0059_spiral_matrix_II__OLD.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
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
res = [[0 for _ in range(n)] for _ in range(n)]
lim = n * n + 1
cnt = 1
i, j, d = 0, 0, 0
while True:
while j < n - d: # top
res[i][j] = cnt
cnt += 1
j += 1
if cnt == lim:
break
i += 1
j -= 1
while i < n - d: # right
res[i][j] = cnt
cnt += 1
i += 1
if cnt == lim:
break
i -= 1
j -= 1
while j > d - 1: # bottom
res[i][j] = cnt
cnt += 1
j -= 1
if cnt == lim:
break
i -= 1
j += 1
d += 1 # update bound at end of cycle
while i > d - 1:
res[i][j] = cnt
cnt += 1
i -= 1
if cnt == lim:
break
i += 1
j += 1
return res