-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
47 lines (40 loc) · 1 KB
/
solution.ts
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
/*
* @lc app=leetcode id=498 lang=javascript
*
* [498] Diagonal Traverse
*/
/**
* @param {number[][]} matrix
* @return {number[]}
*/
const findDiagonalOrder = (matrix: number[][]): number[] => {
// * empty
if (!matrix.length || !matrix[0].length) return [];
// * one line
if (matrix.length === 1) return [...matrix[0]];
// * one row
if (matrix[0].length === 1) return matrix.map((e) => e[0]);
const rowMax = matrix.length - 1;
const columnMax = matrix[0].length - 1;
const result = [];
let row = 1;
let column = -1;
let up = true;
// * walk
while (!(row === rowMax && column === columnMax)) {
if (up) row--, column++;
else row++, column--;
if (row < 0 || column > columnMax) {
up = false;
row++;
if (column > columnMax) row++, column--;
} else if (row > rowMax || column < 0) {
up = true;
column++;
if (row > rowMax) row--, column++;
}
result.push(matrix[row][column]);
}
return result;
};
export { findDiagonalOrder };