-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
36 lines (30 loc) · 945 Bytes
/
main.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
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
directions = ((-1, 1), (1, -1))
m, n = len(mat), len(mat[0])
result = []
i, j = 0, 0
d = 0
for _ in range(m * n):
result.append(mat[i][j])
ii = i + directions[d][0]
jj = j + directions[d][1]
if 0 <= ii < m and 0 <= jj < n:
i, j = ii, jj
continue
d = (d + 1) % 2
if d == 1:
ii = i
jj = j + 1
if 0 <= ii < m and 0 <= jj < n:
i, j = ii, jj
else:
i, j = i + 1, j
else: # d == 0
ii = i + 1
jj = j
if 0 <= ii < m and 0 <= jj < n:
i, j = ii, jj
else:
i, j = i, j + 1
return result