-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54. Spiral Matrix.py
59 lines (54 loc) · 1.96 KB
/
54. Spiral Matrix.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
# @Time : 2019/3/15 18:42
# @Author : xulzee
# @Email : [email protected]
# @File : 54. Spiral Matrix.py
# @Software: PyCharm
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if matrix == []:
return []
start_row, start_column = 0, 0
end_row, end_column = len(matrix) - 1, len(matrix[0]) - 1
res = []
while start_column <= end_column and start_row <= end_row:
self.printEdge(matrix, start_row, start_column, end_row, end_column, res)
start_column += 1
start_row += 1
end_column -= 1
end_row -= 1
return res
def printEdge(self, matrix: list, start_row: int, start_column: int, end_row: int, end_column: int, res: List):
if start_row == end_row:
while start_column <= end_column:
res.append(matrix[start_row][start_column])
start_column += 1
elif start_column == end_column:
while start_row <= end_row:
res.append(matrix[start_row][start_column])
start_row += 1
else:
cur_row = start_row
cur_column = start_column
while cur_column < end_column:
res.append(matrix[cur_row][cur_column])
cur_column += 1
while cur_row < end_row:
res.append(matrix[cur_row][cur_column])
cur_row += 1
while cur_column > start_column:
res.append(matrix[cur_row][cur_column])
cur_column -= 1
while cur_row > start_row:
res.append(matrix[cur_row][cur_column])
cur_row -= 1
if __name__ == '__main__':
# A = [[5, 1, 9, 11],
# [2, 4, 8, 10],
# [13, 3, 6, 7],
# [15, 14, 12, 16]]
A = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
res = []
res = Solution().spiralOrder(A)
print(res)