-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathDiagonalTraverse498.java
69 lines (64 loc) · 1.71 KB
/
DiagonalTraverse498.java
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
/**
* 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:
* https://leetcode.com/static/images/problemset/diagonal_traverse.png
*
* Note:
* The total number of elements of the given matrix will not exceed 10,000.
*/
public class DiagonalTraverse498 {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return new int[0];
int M = matrix.length;
int N = matrix[0].length;
int total = M * N;
int[] res = new int[total];
int i = 0;
int[] pos = new int[2];
res[i++] = matrix[0][0];
while (i < total) {
pos = next(pos, M, N);
res[i++] = matrix[pos[0]][pos[1]];
}
return res;
}
private int[] next(int[] pos, int M, int N) {
int i = pos[0];
int j = pos[1];
if (isEven(i+j)) { // top-right
int ni = i - 1;
int nj = j + 1;
if (nj == N) {
nj = N - 1;
ni = i + 1;
} else if (ni < 0) {
ni = 0;
}
return new int[]{ni, nj};
} else { // bottom-left
int ni = i + 1;
int nj = j - 1;
if (ni == M) {
ni = M - 1;
nj = j + 1;
} else if (nj < 0) {
nj = 0;
}
return new int[]{ni, nj};
}
}
private boolean isEven(int x) {
return x % 2 == 0;
}
}