-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-2-merge-two-sorted-arrays.cpp
More file actions
37 lines (37 loc) · 1.13 KB
/
Copy pathDay-2-merge-two-sorted-arrays.cpp
File metadata and controls
37 lines (37 loc) · 1.13 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
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
// gap method:
int totalLen = m + n;
int gap = (totalLen / 2) + (totalLen % 2);
while(gap > 0){
int i=0, j=gap+i;
while(j < totalLen){
// if i and j are on different indices
if(i<m && j>=m){
if(nums1[i] > nums2[j-m]){
swap(nums1[i], nums2[j-m]);
}
}
// if i and j both are on arr2
else if(i>=m){
if(nums2[i-m] > nums2[j-m]){
swap(nums2[i-m], nums2[j-m]);
}
}
// if i and j both are on arr1
else{
if(nums1[i] > nums1[j]){
swap(nums1[i], nums1[j]);
}
}
i++; j++;
}
if(gap == 1) break;
gap = (gap / 2) + (gap % 2);
}
for(int i=0; i<nums2.size(); i++){
nums1[m+i]=nums2[i];
}
}
};