-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindDiagonalOrder.java
More file actions
31 lines (31 loc) · 1.08 KB
/
findDiagonalOrder.java
File metadata and controls
31 lines (31 loc) · 1.08 KB
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
class Solution {
// Sbke indexes stored kie uske baad baari baari bottom se up aaye from left to right in column wise and element get krke stroe krte gye unke respective indexes pr
public int[] findDiagonalOrder(List<List<Integer>> nums) {
HashMap<Integer,List<Integer>> map=new HashMap<>();
int size=0;
for(int row=nums.size()-1;row>=0;row--){
for(int col=0;col<nums.get(row).size();col++){
int index=row+col;
if(map.containsKey(index)){
map.get(index).add(nums.get(row).get(col));
}
else{
List<Integer> list=new ArrayList<>();
list.add(nums.get(row).get(col));
map.put(index,list);
}
size++;
}
}
int[] ans=new int[size];
int current=0;
int index=0;
while(map.containsKey(current)){
for(int ele:map.get(current)){
ans[index++]=ele;
}
current++;
}
return ans;
}
}