-
Notifications
You must be signed in to change notification settings - Fork 0
/
59.py
30 lines (26 loc) · 792 Bytes
/
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
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
res = [[0 for i in range(n)] for j in range(n)]
num = 0
for i in range(n / 2):
for j in range(i, n - i - 1):
num += 1
res[i][j] = num
for j in range(i, n - i - 1):
num += 1
res[j][n - i - 1] = num
for j in range(n - i - 1, i, -1):
num += 1
res[n - i - 1][j] = num
for j in range(n - i - 1, i, -1):
num += 1
res[j][i] = num
if n % 2 == 1:
res[n / 2][n / 2] = pow(n, 2)
return res
solu = Solution()
print solu.generateMatrix(3)