-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrixII.py
More file actions
43 lines (37 loc) · 1.56 KB
/
SpiralMatrixII.py
File metadata and controls
43 lines (37 loc) · 1.56 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
class Solution:
# @return a list of lists of integer
def generateMatrix(self, n):
result = [[0]*n for x in xrange(n)]
left_margin = top_margin = 0
right_margin = bottom_margin = n - 1
current_elem = 1
while left_margin <= right_margin:
current_row_index = top_margin
current_col_index = left_margin
while current_col_index <= right_margin:
result[current_row_index][current_col_index] = current_elem
current_elem += 1
current_col_index += 1
current_col_index -= 1
current_row_index += 1
while current_row_index <= bottom_margin:
result[current_row_index][current_col_index] = current_elem
current_elem += 1
current_row_index += 1
current_col_index -= 1
current_row_index -= 1
while current_col_index >= left_margin:
result[current_row_index][current_col_index] = current_elem
current_elem += 1
current_col_index -= 1
current_col_index += 1
current_row_index -= 1
while current_row_index > top_margin:
result[current_row_index][current_col_index] = current_elem
current_elem += 1
current_row_index -= 1
top_margin += 1
bottom_margin -= 1
left_margin += 1
right_margin -= 1
return result