-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2018-12-17-59-Spiral-Matrix-II.py
More file actions
44 lines (40 loc) · 1.23 KB
/
2018-12-17-59-Spiral-Matrix-II.py
File metadata and controls
44 lines (40 loc) · 1.23 KB
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
43
44
# -*- coding: utf-8 -*-
# @Author: 何睿
# @Create Date: 2018-12-17 08:57:56
# @Last Modified by: 何睿
# @Last Modified time: 2018-12-17 09:34:05
class Solution:
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
num = 1
res = [[0 for _ in range(n)] for _ in range(n)]
rowtop, coleft, rowbot, colright = 0, 0, n-1, n-1
while rowbot > rowtop and coleft < colright:
for i in range(coleft, colright):
res[rowtop][i] = num
num += 1
for i in range(rowtop, rowbot):
res[i][colright] = num
num += 1
for i in range(colright, coleft, -1):
res[rowbot][i] = num
num += 1
for i in range(rowbot, rowtop, -1):
res[i][coleft] = num
num += 1
rowtop += 1
rowbot -= 1
coleft += 1
colright -= 1
if n % 2:
for i in range(coleft, colright+1):
res[rowtop][i] = num
num += 1
return res
if __name__ == "__main__":
so = Solution()
res = so.generateMatrix(3)
print(res)