-
Notifications
You must be signed in to change notification settings - Fork 2
/
DiagonalTraverse.java
69 lines (60 loc) · 1.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
package Leetcode;
import java.util.Arrays;
/**
* @author kalpak
*
* 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]
*
* Note:
* The total number of elements of the given matrix will not exceed 10,000.
*
*/
public class DiagonalTraverse {
public static int[] findDiagonalOrder(int[][] matrix) {
if (matrix == null || matrix.length == 0)
return new int[0];
int[] result = new int[matrix.length * matrix[0].length];
int row = 0;
int col = 0;
for(int i = 0; i < matrix.length * matrix[0].length; i++) {
result[i] = matrix[row][col];
// Even sum diagonal
if ((row + col) % 2 == 0) {
if (row - 1 >= 0 && col + 1 < matrix[0].length) {
row = row - 1;
col = col + 1;
} else if (col + 1 < matrix[0].length) {
col = col + 1;
} else {
row = row + 1;
}
} else if ((row + col) % 2 != 0){
if (row + 1 < matrix.length && col - 1 >= 0) {
row = row + 1;
col = col - 1;
} else if (row + 1 < matrix.length) {
row = row + 1;
} else {
col = col + 1;
}
}
}
return result;
}
public static void main(String[] args) {
int[][] arr = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
System.out.println(Arrays.toString(findDiagonalOrder(arr)));
}
}