-
Notifications
You must be signed in to change notification settings - Fork 90
/
Matrix Zigzag Traversal.py
62 lines (54 loc) · 1.35 KB
/
Matrix Zigzag Traversal.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
60
61
62
"""
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-order
Example
Given a matrix:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10, 11, 12]
]
return [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12]
"""
__author__ = 'Daniel'
class Solution:
def printZMatrix(self, matrix):
"""
:param matrix: a matrix of integers
:return: a list of integers
"""
i = 0
j = 0
m = len(matrix)
n = len(matrix[0])
ret = []
up = True
for _ in xrange(m*n):
ret.append(matrix[i][j])
if up:
if i-1<0 or j+1>=n:
up = False
if j+1>=n: # go down
i += 1
else: # go right
j += 1
else:
i -= 1
j += 1
else:
if i+1>=m or j-1<0:
up = True
if i+1>=m:
j += 1 # go right
else:
i += 1 # go up
else:
i += 1
j -= 1
return ret
if __name__=="__main__":
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
print Solution().printZMatrix(matrix)