- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1424.java
29 lines (27 loc) · 1.07 KB
/
_1424.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.*;
publicclass_1424 {
publicstaticclassSolution1 {
/*
* One key note:
* For all elements on the same diagonal, the sums of their row index and column index are equal.
* This is widely applicable to all matrix problems.
*/
publicint[] findDiagonalOrder(List<List<Integer>> nums) {
TreeMap<Integer, List<Integer>> map = newTreeMap<>();
for (inti = 0; i < nums.size(); i++) {
for (intj = 0; j < nums.get(i).size(); j++) {
intindex = i + j;
List<Integer> list = map.getOrDefault(index, newArrayList<>());
list.add(0, nums.get(i).get(j));
map.put(index, list);
}
}
List<Integer> list = newArrayList<>();
for (intindex : map.keySet()) {
list.addAll(map.get(index));
}
returnlist.stream().mapToInt(Integer -> Integer).toArray();
}
}
}