Problem 88 Leetcode
Merge Sorted Arraysunknown
c_cpp
2 years ago
841 B
18
Indexable
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int insertIndex = 0;
int i = 0, j = 0;
vector<int> res;
// 1 4 8 8 9 -> i -> i+1 -> i+2
// 2 3 5 6 7 -> j -> j+1 -> j+2 -> j+3 -> j+4
// 1 2 3 4 5 6 7
// nums1 -> length -> m + n
while (i < m && j < n) {
if (nums1[i] <= nums2[j]) {
res.push_back(nums1[i]);
i++;
} else {
res.push_back(nums2[j]);
j++;
}
}
while (i < m) {
res.push_back(nums1[i]);
i++;
}
while (j < n) {
res.push_back(nums2[j]);
j++;
}
nums1 = res;
}
};Editor is loading...