88 Merge Sorted Array(归并排序Easy)
题目意思:num1和num2均为递增数组,对其进行递增排序存到num1中
1 class Solution { 2 public: 3 void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { 4 vector<int> nums3(nums1); //挺好用,vector克隆 5 int index1=0,index2=0,index3=0; 6 while(index2<n&&index3<m){ 7 if(nums2[index2]<=nums3[index3]){ 8 nums1[index1++]=nums2[index2++]; 9 } 10 else{ 11 nums1[index1++]=nums3[index3++]; 12 } 13 } 14 while(index2<n){ 15 nums1[index1++]=nums2[index2++]; 16 } 17 while(index3<m){ 18 nums1[index1++]=nums3[index3++]; 19 } 20 } 21 };
时间复杂度:O(n)
运行时间:4ms