-
Notifications
You must be signed in to change notification settings - Fork 2
/
DiagonalTraverse.java
134 lines (114 loc) · 3.75 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package arrays.multidimensional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 498. Diagonal Traverse
* 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]
*
* IMP-3
*
*/
public class DiagonalTraverse {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
DiagonalTraverse diagonalTraverse = new DiagonalTraverse();
int[] tmp = diagonalTraverse.findDiagonalOrder(matrix);
for (int num : tmp) {
System.out.println(num);
}
}
/**
* this is a simpler approach than having the indexes traverse based on edges. but its slower because you are
* reversing collection and using a place holder collection to store the diagonal values
*
* @param matrix
* @return
*/
public int[] findDiagonalOrder(int[][] matrix) {
int rows = matrix.length;
if (rows > 0) {
int cols = matrix[0].length;
int size = rows * cols;
int[] out = new int[size];
if (cols == 0) return out;
int index = 0;
List<Integer> vals = new ArrayList<>();
for (int di = 0; di < rows + cols; di++) {
int row = 0;
int col;
vals.clear();
col = Math.min(di, cols - 1);
if (col == cols - 1) {
row = di - col;
}
while (row < rows && col >= 0) {
vals.add(matrix[row][col]);
row++;
col--;
}
if (di % 2 == 0) {
Collections.reverse(vals);
}
for (int val : vals) {
out[index++] = val;
}
}
return out;
}
return new int[0];
}
public int[] findDiagonalOrderEdgeBased(int[][] matrix) {
int rows = matrix.length;
if (rows > 0) {
int cols = matrix[0].length;
int size = rows * cols;
int[] out = new int[size];
if (cols == 0) return out;
int index = 0;
int row = 0, col = 0;
boolean upDirection = true;
do {
out[index++] = matrix[row][col];
if (upDirection) {
if (row > 0 && col < cols - 1) {
row--;
col++;
} else if (col < cols - 1) {
col++; //shift one col to right and change direction down
upDirection = false;
} else if (col == cols - 1) {
row++; //wrap to next row
upDirection = false;
}
} else {
if (row < rows - 1 && col > 0) {
row++;
col--;
} else if (col == 0 && row < rows - 1) {
row++; //shift one row down and change direction up
upDirection = true;
} else if (row == rows - 1) {
col++; //wrap to next col
upDirection = true;
}
}
if (row >= rows || col >= cols || row < 0 || col < 0) break;
} while ((row != matrix.length - 1) || (col != cols - 1));
out[index] = matrix[row][col]; //last col
return out;
}
return new int[0];
}
}