-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2570.java
43 lines (41 loc) · 1.46 KB
/
_2570.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
37
38
39
40
41
42
43
package com.fishercoder.solutions.thirdthousand;
import java.util.ArrayList;
import java.util.List;
public class _2570 {
public static class Solution1 {
public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
List<int[]> mergedList = new ArrayList<>();
int i1 = 0;
int i2 = 0;
for (; i1 < nums1.length && i2 < nums2.length; ) {
int id1 = nums1[i1][0];
int id2 = nums2[i2][0];
if (id2 == id1) {
mergedList.add(new int[] {id1, nums1[i1][1] + nums2[i2][1]});
i1++;
i2++;
} else if (id1 < id2) {
mergedList.add(new int[] {id1, nums1[i1][1]});
i1++;
} else {
mergedList.add(new int[] {id2, nums2[i2][1]});
i2++;
}
}
while (i1 < nums1.length) {
mergedList.add(new int[] {nums1[i1][0], nums1[i1][1]});
i1++;
}
while (i2 < nums2.length) {
mergedList.add(new int[] {nums2[i2][0], nums2[i2][1]});
i2++;
}
int[][] ans = new int[mergedList.size()][2];
for (int i = 0; i < mergedList.size(); i++) {
ans[i][0] = mergedList.get(i)[0];
ans[i][1] = mergedList.get(i)[1];
}
return ans;
}
}
}