forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMerge Sorted Array.cpp
More file actions
32 lines (32 loc) · 823 Bytes
/
Merge Sorted Array.cpp
File metadata and controls
32 lines (32 loc) · 823 Bytes
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
// Runtime: 3 ms (Top 76.26%) | Memory: 9.3 MB (Top 30.17%)
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
vector<int> temp(n+m);
int i=0,j=0,k=0;
while(i<m && j<n){ //select smaller element from both vector
if(nums1[i]<nums2[j]){
temp[k]=nums1[i];
i++;
k++;
}
else{
temp[k]=nums2[j];
j++;
k++;
}
}
while(i<m){ //insert remaining nums1 element
temp[k]=nums1[i];
i++;
k++;
}
while(j<n){ //insert remaining nums2 element
temp[k]=nums2[j];
j++;
k++;
}
nums1=temp;
return ;
}
};