-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_2570_MergeTwo2DArraysbySummingValues
More file actions
45 lines (40 loc) · 1.12 KB
/
LC_2570_MergeTwo2DArraysbySummingValues
File metadata and controls
45 lines (40 loc) · 1.12 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
int id1 = nums1.length;
int id2 = nums2.length;
Map<Integer,Integer> map = new TreeMap<>();
int p1 = 0;
int p2 = 0;
while(p1 < id1 && p2 < id2){
if(nums1[p1][0] == nums2[p2][0]){
map.put(nums1[p1][0], nums1[p1][1] + nums2[p2][1]);
p1++;
p2++;
}
else if(nums1[p1][0] < nums2[p2][0]){
map.put(nums1[p1][0], nums1[p1][1]);
p1++;
}
else{
map.put(nums2[p2][0], nums2[p2][1]);
p2++;
}
}
while(p1 < id1){
map.put(nums1[p1][0], nums1[p1][1]);
p1++;
}
while(p2 < id2){
map.put(nums2[p2][0], nums2[p2][1]);
p2++;
}
int[][] ans = new int[map.size()][2];
int k = 0;
for(var e : map.entrySet()){
ans[k][0] = e.getKey();
ans[k][1] = e.getValue();
k++;
}
return ans;
}
}