forked from ChienJuiLin/leetcode_heprecsler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_4.py
More file actions
71 lines (49 loc) · 1.5 KB
/
leetCode_4.py
File metadata and controls
71 lines (49 loc) · 1.5 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
71
'''
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Example:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
Explanation:
Note:
The total number of elements of the given matrix will not exceed 10,000.
'''
class Solution:
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == []: return []
t = len(matrix) * len(matrix[0])
rlen = len(matrix)
clen = len(matrix[0])
r,c = 0,0
move = 1
ans = list()
for i in range(t):
ans.append(matrix[r][c])
next_r = r - move
next_c = c + move
if rlen-1 < next_r or next_r < 0 or clen-1 < next_c or next_c < 0:
if move > 0:
next_r = r
next_c = c + 1
if rlen-1 < next_r or next_r < 0 or clen-1 < next_c or next_c < 0:
next_r = r + 1
next_c = c
move = move * (-1)
else:
next_r = r + 1
next_c = c
if rlen-1 < next_r or next_r < 0 or clen-1 < next_c or next_c < 0:
next_r = r
next_c = c + 1
move = move * (-1)
r = next_r
c = next_c
return ans