-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path59.py
More file actions
42 lines (38 loc) · 989 Bytes
/
59.py
File metadata and controls
42 lines (38 loc) · 989 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
39
40
41
42
class Solution:
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
ans = [[0]*n for _ in range(n)]
x, y, row, column = 0, 0, n-1, n-1
nums = 1
while x <= row and y <= column:
i = x
while i <= column:
ans[x][i] = nums
nums += 1
i = i+1
i = x+1
while i <= row:
ans[i][column] = nums
nums += 1
i = i+1
i = column-1
while i >= y and y != column:
ans[row][i] = nums
nums += 1
i -= 1
i = row - 1
while i > x and x != row:
ans[i][y] = nums
nums += 1
i -= 1
x += 1
y += 1
row -= 1
column -= 1
return ans
n = 3
test = Solution()
print(test.generateMatrix(n))