-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (23 loc) · 770 Bytes
/
Copy pathSolution.java
File metadata and controls
29 lines (23 loc) · 770 Bytes
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
//Time Complexity: O(m * n)
//Space Complexity: O(1)
public class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix == null || matrix.length == 0) return new int[0];
int m = matrix.length, n = matrix[0].length;
int[] result = new int[m * n];
int row = 0, col = 0;
for (int i = 0; i < m * n; i++) {
result[i] = matrix[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;
}
}