-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
39 lines (36 loc) · 1.06 KB
/
Main.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
package com.company;
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
findDiagonalOrder(matrix);
}
public static int[] findDiagonalOrder(int[][] matrix) {
if(matrix.length == 0 || matrix[0].length == 0) return new int[0];
int[][] dir = {{-1, 1}, {1, -1}};
int x = 0, y = 0, index = 0, row = matrix.length, col = matrix[0].length;
int[] res = new int[row * col];
for (int i = 0; i < res.length; i++) {
res[i] = matrix[x][y];
x += dir[index % 2][0];
y += dir[index % 2][1];
if (x >= 0 && x < row && y >= 0 && y < col) continue;
if (x >= row) {
x--;
y += 2;
} else if (y >= col) {
y--;
x += 2;
}
if (x < 0)
x++;
else if (y < 0)
y++;
index++;
}
return res;
}
}