-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDiagonal Traverse II.java
36 lines (34 loc) · 982 Bytes
/
Diagonal Traverse II.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
class Solution {
public int[] findDiagonalOrder(List<List<Integer>> nums) {
HashMap<Integer , Stack<Integer>> map = new HashMap();
for(int i = 0 ; i < nums.size() ; i++){
for(int j = 0 ; j < nums.get(i).size() ; j++){
int z = i + j;
if(map.containsKey(z)){
map.get(z).add(nums.get(i).get(j));
}else{
Stack<Integer> stk = new Stack<>();
stk.push(nums.get(i).get(j));
map.put(z , stk);
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
int k = 0;
while(true){
if(map.containsKey(k)){
int size = map.get(k).size();
while(size-- > 0){
arr.add(map.get(k).pop());
}
k++;
}else{
break;
}
}
int[] res = new int[arr.size()];
for(int i = 0 ; i < res.length ; i++){
res[i] = arr.get(i);
}
return res;
}}