-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagonalTraverse.java
73 lines (63 loc) · 1.96 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
import java.util.Arrays;
/**
*
*/
public class DiagonalTraverse {
public static void main(String[] args) {
DiagonalTraverse sol = new DiagonalTraverse();
System.out.println(Arrays.toString(sol.findDiagonalOrder(new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}))); // [1,2,4,7,5,3,6,8,9]
}
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 min = Math.min(m,n);
int max = Math.max(m,n);
int[] res = new int[m * n];
int dir = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// int diagonalLen =
}
}
int i = 0;
int j = 0;
boolean up = true;
// Traverse the matrix till all elements get traversed
for (int k = 0; k < m * n;) {
// If up is true then traverse from downward to upward
if (up) {
for (; i >= 0 && j < n; j++, i--) {
res[k++] = matrix[i][j];
}
// Set i and j according to direction
if (i < 0 && j <= n - 1)
i = 0;
if (j == n) {
i = i + 2;
j--;
}
}
// If up is 0 then traverse up to down
else {
for (; j >= 0 && i < m; i++, j--) {
res[k++] = matrix[i][j];
}
// Set i and j according to direction
if (j < 0 && i <= m - 1)
j = 0;
if (i == m) {
j = j + 2;
i--;
}
}
// Revert the up to change the direction
up = !up;
}
return res;
}
}