-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral-matrix-II.py
49 lines (40 loc) · 1.04 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
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
length = n
arr = [[0] * n for _ in range(n)]
i, j = 0, 0
di, dj = 1, 0
k = 1
steps = n-k
countdown = 2
curr = 1
sq = n ** 2
while curr < sq:
arr[j][i] = curr
curr += 1
i += di
j += dj
steps -= 1
if countdown == 0:
k += 1
countdown = 2
if steps == 0:
di, dj = self.turnRight(di, dj)
steps = n - k
countdown -= 1
if n % 2 != 0 :
arr[i][j] = curr
else:
arr[i+1][j-1] = curr
return arr
def turnRight(self, di, dj):
if di == 0:
if dj == 1:
return -1, 0
else:
return 1, 0
else:
if di == 1:
return 0, 1
else:
return 0, -1