-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path59_Spiral_Matrix_II.py
More file actions
86 lines (61 loc) · 1.5 KB
/
59_Spiral_Matrix_II.py
File metadata and controls
86 lines (61 loc) · 1.5 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
75
76
77
78
79
80
81
82
83
84
85
86
"""
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
4:
[[1, 2 ,3,4],
[12,13,14,5],
[11,16,15,6],
[10, 9, 8,7]
]
"""
class Solution:
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
top, bottom = 0, n-1
left,right = 0, n-1
k = 0
rs = [[0 for i in range(n)] for j in range(n)]
while top <= bottom and left <= right:
for i in range(left,right+1):
k += 1
rs[top][i] = k
top += 1
for i in range(top,bottom+1):
k += 1
rs[i][right] = k
right -= 1
for i in range(right,left-1,-1):
if top <= bottom:
k += 1
rs[bottom][i] = k
bottom -= 1
for i in range(bottom,top-1,-1):
if left <= right:
k += 1
rs[i][left] = k
left += 1
return rs
A = [[0] * n for _ in range(n)]
i, j, di, dj = 0, 0, 0, 1
for k in range(n*n):
print(i,j,di,dj,k+1)
A[i][j] = k + 1
if A[(i+di)%n][(j+dj)%n]:
print('---in')
di, dj = dj, -di
i += di
j += dj
return A
so = Solution()
n = 4
print(so.generateMatrix(n))