-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path59_SpiralMatrixII.py
More file actions
69 lines (55 loc) · 1.55 KB
/
59_SpiralMatrixII.py
File metadata and controls
69 lines (55 loc) · 1.55 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# coding: utf8
"""
题目链接:
题目描述:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
"""
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
if n <= 0:
return []
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
sx, sy = 0, 0
digit = 1
dr = 0
changes = 1
circle = 0
rows = columns = n
total = rows * columns
matrix = [{} for _ in range(rows)]
while total > 0:
matrix[sx][sy] = digit
if dr == 0 and sy == columns - circle - 1:
dr = 1
changes += 1
elif dr == 1 and sx == rows - circle - 1:
dr = 2
changes += 1
elif dr == 2 and sy == circle:
dr = 3
changes += 1
elif dr == 3 and sx == circle:
dr = 0
changes += 1
dx, dy = directions[dr]
sx, sy = sx + dx, sy + dy
if changes == 4:
circle += 1
changes = 0
total -= 1
digit += 1
for idx, sub_matrix in enumerate(matrix):
matrix[idx] = [sub_matrix[key] for key in sorted(sub_matrix.keys())]
return matrix