-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral_Order_Matrix_II.py
More file actions
38 lines (36 loc) · 1007 Bytes
/
Copy pathSpiral_Order_Matrix_II.py
File metadata and controls
38 lines (36 loc) · 1007 Bytes
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
class Solution:
# @param A : integer
# @return a list of list of integers
def generateMatrix(self, A):
M = [0] * A
for i in range(A):
M[i] = [0] * A
T = 0
B = A-1
L = 0
R = A-1
dir = 0
count = 1
while (T<=B and L<=R):
if dir == 0:
for i in range(L,R+1):
M[T][i] = count
count += 1
T += 1
elif dir == 1:
for i in range(T,B+1):
M[i][R] = count
count += 1
R -= 1
elif dir == 2:
for i in range(R,L-1,-1):
M[B][i] = count
count += 1
B -= 1
elif dir == 3:
for i in range(B,T-1,-1):
M[i][L] = count
count += 1
L += 1
dir = (dir+1) % A
return M