-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_59.py
37 lines (35 loc) · 1.26 KB
/
array_59.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
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
if n <= 0:
return []
result = [[0] * n for _ in range(n)]
start_row, end_row, start_col, end_col = 0, n-1, 0, n-1
number = 1
while start_row <= end_row and start_col <= end_col:
for i in range(start_col, end_col+1):
result[start_row][i] = number
number += 1
if end_row > start_row:
for i in range(start_row + 1, end_row + 1):
result[i][end_col] = number
number += 1
if end_row > start_row and end_col > start_col:
for i in range(end_col-1, start_col-1, -1):
result[end_row][i] = number
number += 1
if end_row - 1 > start_row and end_col > start_col:
for i in range(end_row-1, start_row, -1):
result[i][start_col] = number
number += 1
start_row +=1
end_row -= 1
start_col += 1
end_col -= 1
return result
if __name__ == '__main__':
solution = Solution()
print(solution.generateMatrix(1))