-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54.py
More file actions
50 lines (45 loc) · 1.09 KB
/
54.py
File metadata and controls
50 lines (45 loc) · 1.09 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
class Solution:
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix:
return []
ans = list()
x, y, row, column = 0, 0, len(matrix)-1, len(matrix[0])-1
while x <= row and y <= column:
i = y
while i <= column:
ans.append(matrix[x][i])
i += 1
i = x+1
while i < row:
ans.append(matrix[i][column])
i += 1
i = column
while i >= y and x != row:
ans.append(matrix[row][i])
i -= 1
i = row-1
while i > x and y != column:
ans.append(matrix[i][y])
i = i-1
y += 1
x += 1
row -= 1
column -= 1
return ans
nums = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
nums1 = [[2, 3]]
nums2 = [[2], [3]]
nums3 = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
test = Solution()
print(test.spiralOrder(nums3))