(lleetcode)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 nums1and nums2 are m and n respectively.

使用快排进行排序:

 class Solution {
public: void quicksort(vector<int>& a,int left,int right)
{
if(left>right)
return;
int piv = a[left];
int i = left;
int j = right; while(i!=j)
{
//从右边开始
while(a[j] >= piv && i < j)
j--;
while(a[i] <= piv && i < j)
i++; if(i<j)
{
/*int t = a[i];
a[i]=a[j];
a[j]=t;*/
swap(a[i],a[j]);
}
}
a[left] = a[i];
a[i] = piv; quicksort(a,left,i-);
quicksort(a,i+,right);
}
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i,j;
for(i = m,j=;i<m+n;++i)
{
nums1[i] = nums2[j++];
}
quicksort(nums1,,m+n-);
}
};
上一篇:Lua 的函数库 01


下一篇:Android开发拾遗(一)用Wi-Fi连接adb