-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiagonalTraverse498.java
More file actions
30 lines (29 loc) · 876 Bytes
/
DiagonalTraverse498.java
File metadata and controls
30 lines (29 loc) · 876 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
30
import java.util.*;
public class DiagonalTraverse498 {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[] res = new int[m * n];
HashMap<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int key = i + j;
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(mat[i][j]);
}
}
int i = 0;
int count = 0;
for (Integer x : map.keySet()) {
if (count % 2 != 0) {
Collections.reverse(map.get(x));
}
for (Integer y : map.get(x)) {
res[i++] = y;
}
count++;
}
return res;
}
}