| 1 | class Solution { |
| 2 | public int[][] mergeArrays(int[][] nums1, int[][] nums2) { |
| 3 | ArrayList<int[]> res = new ArrayList<>(); |
| 4 | int n = nums1.length; |
| 5 | int m = nums2.length; |
| 6 | int i=0; |
| 7 | int j=0; |
| 8 | while(i<n && j<m){ |
| 9 | if(nums1[i][0] == nums2[j][0]){ |
| 10 | res.add(new int[]{nums1[i][0], nums1[i][1] + nums2[j][1] }); |
| 11 | i++; |
| 12 | j++; |
| 13 | }else if(nums1[i][0] < nums2[j][0]){ |
| 14 | res.add(new int[]{nums1[i][0], nums1[i][1]}); |
| 15 | i++; |
| 16 | }else{ |
| 17 | res.add(new int[]{nums2[j][0],nums2[j][1] }); |
| 18 | j++; |
| 19 | } |
| 20 | } |
| 21 | while(i<n){ |
| 22 | res.add(new int[]{nums1[i][0], nums1[i][1]}); |
| 23 | i++; |
| 24 | } |
| 25 | |
| 26 | while(j<m){ |
| 27 | res.add(new int[]{nums2[j][0],nums2[j][1] }); |
| 28 | j++; |
| 29 | } |
| 30 | int len = res.size(); |
| 31 | int result[][] = new int[len][2]; |
| 32 | for(int ind=0;ind<len;ind++){ |
| 33 | result[ind] = res.get(ind); |
| 34 | } |
| 35 | return result; |
| 36 | } |
| 37 | } |