88. Merge Sorted Array
problem description
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]algorithm thought
code
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i=m-1,j=n-1,res=m+n-1;
while(j>=0){
nums1[res--]= i>=0&&nums1[i]>nums2[j]?nums1[i--]:nums2[j--];
}
}
};algorithm analysis
Last updated