-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge_sorted_array.cpp
More file actions
53 lines (45 loc) · 1.26 KB
/
merge_sorted_array.cpp
File metadata and controls
53 lines (45 loc) · 1.26 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
46
47
48
49
50
51
52
53
//program to merge sorted array
//problem link: https://leetcode.com/problems/merge-sorted-array/
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
if(m == 0)
{
nums1 = nums2;
return;
}
// make a array which will contain 2 array's all value
vector<int>full_vector;
nums1.push_back(INT_MAX);
nums2.push_back(INT_MAX);
nums1[m] = INT_MAX;
nums2[n]= INT_MAX;
int nums1_index = 0, nums2_index = 0;
for(int i = 0; i< n+m; i++)
{
if(nums1[nums1_index] < nums2[nums2_index])
{
full_vector.push_back(nums1[nums1_index]);
nums1_index ++;
}
else
{
full_vector.push_back(nums2[nums2_index]);
nums2_index ++;
}
}
// for(int i = 0; i< m; i++)
// {
// nums1[i] = full_vector[i];
// }
// int a = 0;
// for(int i = m; i<n; i++)
// {
// nums2[a] = full_vector[i];
// a++;
// }
// nums1.pop_back();
// nums2.pop_back();
nums1 = full_vector;
}
};