-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0054_spiral_matrix_2.py
More file actions
71 lines (64 loc) · 1.99 KB
/
0054_spiral_matrix_2.py
File metadata and controls
71 lines (64 loc) · 1.99 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
70
# **************************************************************************** #
# #
# ::: :::::::: #
# 054_Spiral_Matrix.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: kcheung <kcheung@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/02/10 15:05:22 by kcheung #+# #+# #
# Updated: 2018/02/11 14:57:56 by kcheung ### ########.fr #
# #
# **************************************************************************** #
import pprint
'''
Given a matrix of m x n elements (m rows, n columns), return all elements of the
matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
'''
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
n = len(matrix)
layers = n // 2
result = []
for l in range(layers):
first = l
last = n - l - 1
top = []
right = []
bottom = []
left = []
for i in range(first, last):
offset = i - first
top.append(matrix[first][i])
right.append(matrix[i][last])
bottom.append(matrix[last][last - offset])
left.append(matrix[last - offset][first])
comb = top + right + bottom + left
result += comb
if n % 2 != 0:
x = (n // 2)
result.append(matrix[x][x])
return result
s = Solution()
pp = pprint.PrettyPrinter()
m = 3
n = 5
m = [[i+(j*n) for i in range(1,n+1)] for j in range(m)]
pp.pprint(m)
print(s.spiralOrder(m))
print("")
n = 6
m = [[i+(j*n) for i in range(1,n+1)] for j in range(n)]
pp.pprint(m)
print(s.spiralOrder(m))