forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagonalTraverse.java
81 lines (76 loc) · 2.47 KB
/
DiagonalTraverse.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
70
71
72
73
74
75
76
77
78
79
80
81
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
/**
* 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:
Note:
The total number of elements of the given matrix will not exceed 10,000.
*/
public class DiagonalTraverse {
public int[] findDiagonalOrder(int[][] matrix) {
int[] diagonalOrder = new int[]{};
if (matrix == null || matrix.length == 0) return diagonalOrder;
List<Integer> list = new ArrayList<>();
list.add(matrix[0][0]);
boolean goLeftDownExpand = true, goRightUpExpand = false, goLeftDownShrink = false, goRightUpShrink = false;
int m = matrix.length, n = matrix[0].length;
for (int i = 0, j = 0; list.size() < m*n;) {
if (goLeftDownExpand) {
j++;
while (j >= 0) {
list.add(matrix[i++][j--]);
}
goLeftDownExpand = false;
goRightUpExpand = true;
j++;//recover j to be 0
i--;
if (i == m-1) {
goRightUpShrink = true;
goLeftDownExpand = false;
goRightUpExpand = false;
}
} else if (goRightUpExpand) {
i++;
while (i >= 0) {
list.add(matrix[i--][j++]);
}
goRightUpExpand = false;
goLeftDownExpand = true;
i++;
j--;
if (j == n-1) {
goLeftDownShrink = true;
goRightUpExpand = false;
goLeftDownExpand = false;
}
} else if (goLeftDownShrink) {
while (i < m) {
list.add(matrix[i++][j--]);
}
i--;
goLeftDownShrink = false;
goRightUpShrink = true;
} else if (goRightUpShrink) {
while (j < n) {
list.add(matrix[i--][j++]);
}
j--;
goRightUpShrink = false;
goLeftDownShrink = true;
}
}
for (int i = 0; i < m*n; i++) {
diagonalOrder[i] = list.get(i);
}
return diagonalOrder;
}
}