Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
分析: 注意理解题目 从num1中取前m个 从num2中取前n个 然后放入num1中 O(N)的复杂度即可
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i=0; int j=0; for(; j<n;j++){ for(; i<m+j;i++){ if(nums2[j]<nums1[i]) break; } if(i<m+j){ nums1.insert(nums1.begin()+i,nums2[j]); nums1.pop_back(); } else break; } while(j<n){ nums1.insert(nums1.begin()+m+j-1,nums2[j++]); nums1.pop_back(); } } };