-
Notifications
You must be signed in to change notification settings - Fork 17
/
diagonal-traverse.py
45 lines (32 loc) · 1.31 KB
/
diagonal-traverse.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
from typing import Tuple
def next_state(
row: int, col: int, vector_row: int, vector_col: int, rows: int, cols: int
) -> Tuple[int, int, int, int]:
next_row, next_col = row + vector_row, col + vector_col
if not (0 <= next_row < rows) and not (0 <= next_col < cols):
if (vector_row, vector_col) == (-1, 1):
return row + 1, col, -vector_row, -vector_col
else:
return row, col + 1, -vector_row, -vector_col
elif not (0 <= next_row < rows):
return row, col + 1, -vector_row, -vector_col
elif not (0 <= next_col < cols):
return row + 1, col, -vector_row, -vector_col
return next_row, next_col, vector_row, vector_col
def diagonal_traverse(matrix: List[List[int]]) -> List[int]:
if not matrix:
return []
rows, cols = len(matrix), len(matrix[0]) if matrix else 0
vector_row, vector_col = -1, 1
row, col = 0, 0
result: List[int] = []
while (row, col) != (rows - 1, cols - 1):
result.append(matrix[row][col])
row, col, vector_row, vector_col = next_state(
row, col, vector_row, vector_col, rows, cols
)
result.append(matrix[row][col])
return result
class Solution:
def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]:
return diagonal_traverse(matrix)