-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
45 lines (33 loc) · 1.02 KB
/
Solution.java
File metadata and controls
45 lines (33 loc) · 1.02 KB
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
package leetcode.diagonalTraverse;
import java.util.Arrays;
public class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length, n = mat[0].length;
int j = m * n;
int[] result = new int[j];
int col = 0, row = 0;
for(int i = 0; i < j; i++){
result[i] = mat[row][col];
if((row+col) % 2 == 0){
if(col == n - 1) row++;
else if (row == 0) col++;
else {row--; col++;};
} else {
if(row == m - 1) col++;
else if(col == 0) row++;
else {row++; col--;};
}
}
return result;
}
public static void main(String[] args) {
int[][] board = new int[][] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
};
Solution solution = new Solution();
int[] result = solution.findDiagonalOrder(board);
System.out.println(Arrays.toString(result));
}
}