-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0498-Diagonal-Traverse.py
More file actions
33 lines (32 loc) · 978 Bytes
/
0498-Diagonal-Traverse.py
File metadata and controls
33 lines (32 loc) · 978 Bytes
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
class Solution:
def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]:
m = len(matrix)
n = len(matrix[0]) if m > 0 else 0
if n == 0:
return []
result = [0 for i in range(m*n)]
up = True
row = col = 0
for i in range(m*n):
result[i] = matrix[row][col]
if up:
if col == n - 1:
row = row + 1
up = not up
elif row == 0:
col = col + 1
up = not up
else:
row = row - 1
col = col + 1
else:
if row == m - 1:
col = col + 1
up = not up
elif col == 0:
row = row + 1
up = not up
else:
row = row + 1
col = col - 1
return result