Question:
Given two sorted integer arrays nums1 and nums2,
merge nums2 into nums1 as
one sorted array.
Algorithm:
类似归并排序
Accepted Code:
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n){ vector<int> res; for(int i=0,j=0;i<m||j<n;) { if(i==m) { res.push_back(nums2[j]); j++; } else if(j==n) { res.push_back(nums1[i]); i++; } else if(nums1[i]<nums2[j]) { res.push_back(nums1[i]); i++; } else { res.push_back(nums2[j]); j++; } } nums1=res; } };
时间: 2024-12-13 08:44:56