-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterviewbit-arrays-spiral-order-matrix-ii.py
42 lines (38 loc) · 1.19 KB
/
interviewbit-arrays-spiral-order-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
class Solution:
# @param A : integer
# @return a list of list of integers
def generateMatrix(self, A):
matrix = []
for r in range(A):
row = []
for c in range(A):
row.append(0)
matrix.append(row)
top = left = 0
bottom = right = A - 1
i = 1
r, d, l, u = 0, 1, 2, 3
direction = r
while i <= A**2:
if direction == r:
for col in range(left, right + 1):
matrix[top][col] = i
i += 1
top += 1
elif direction == d:
for row in range(top, bottom + 1):
matrix[row][right] = i
i += 1
right -= 1
elif direction == l:
for col in range(right, left - 1, -1):
matrix[bottom][col] = i
i += 1
bottom -= 1
elif direction == u:
for row in range(bottom, top - 1, -1):
matrix[row][left] = i
i += 1
left += 1
direction = (direction + 1) % 4
return matrix