-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiralMatrix.py
43 lines (41 loc) · 1.12 KB
/
spiralMatrix.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
# Spiral matrix
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
1. Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
2. Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Idea:
while matrix is not null
1. remove the first line of the matrix, and extend the list to result
2. rotate the matrix counterclockwisely
"""
res = []
while matrix:
res.extend(matrix[0])
print("res",res )
matrix.remove(matrix[0])
# 2. rotate the matrix counterclckwise
matrix = list(zip(*matrix))[::-1]
print(matrix)
return res
s = Solution()
matrix = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
print(s.spiralOrder(matrix))