-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path59.py
41 lines (35 loc) · 986 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
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
# init
matrix = []
for i in xrange(0, n):
l = list()
for j in xrange(0, n):
l.append(0)
matrix.append(l)
num = 1
i = 0
while i < n/2:
for j in xrange(i, n-i-1):
matrix[i][j] = num
num += 1
for j in xrange(i, n-i-1):
matrix[j][n-i-1] = num
num += 1
for j in xrange(n-i-1, i, -1):
matrix[n-i-1][j] = num
num += 1
for j in xrange(n-i-1, i, -1):
matrix[j][i] = num
num += 1
i += 1
if n % 2 == 1:
matrix[n/2][n/2] = num
return matrix