-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalTraverse.java
More file actions
25 lines (25 loc) · 835 Bytes
/
DiagonalTraverse.java
File metadata and controls
25 lines (25 loc) · 835 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
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[] result = new int[m * n];
Map<Integer, List<Integer>> diagonalMap = new HashMap<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int key = i + j;
diagonalMap.putIfAbsent(key, new ArrayList<>());
diagonalMap.get(key).add(mat[i][j]);
}
}
int index = 0;
for (int key = 0; key < m + n - 1; key++) {
List<Integer> diagonal = diagonalMap.get(key);
if (key % 2 == 0) {
Collections.reverse(diagonal);
}
for (int num : diagonal) {
result[index++] = num;
}
}
return result;
}
}