forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0059.py
More file actions
24 lines (21 loc) · 703 Bytes
/
0059.py
File metadata and controls
24 lines (21 loc) · 703 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
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
ans = [[0 for j in range(n)] for i in range(n)]
count = 1
for min in range(n // 2):
max = n - min - 1
for i in range(min, max):
ans[min][i] = count
count += 1
for i in range(min, max):
ans[i][max] = count
count += 1
for i in range(max, min, -1):
ans[max][i] = count
count += 1
for i in range(max, min, -1):
ans[i][min] = count
count += 1
if n & 1:
ans[n // 2][n // 2] = count
return ans