class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
quick_sort(nums1,0,nums1.size()-1);
quick_sort(nums2,0,nums2.size()-1);
vector<int> v;
int i = 0;
int fast = 0;
while(i<nums1.size()&&fast < nums2.size())
{
if(nums1[i] < nums2[fast])
{
i++;
}
else if(nums1[i] == nums2[fast])
{
v.push_back(nums1[i]);
i++;
fast++;
}
else
{
fast++;
}
}
return v;
}
int partition(vector<int> &v,int low,int high)
{
int tmp = v[low];
while(low < high)
{
while(low < high && v[high] >= tmp) high--;
v[low] = v[high];
while(low < high && v[low] <= tmp) low++;
v[high] = v[low];
}
v[low] = tmp;
return low;
}
void quick_sort(vector<int> &v,int low,int high)
{
if(low < high)
{
int mid = partition(v,low,high);
quick_sort(v,low,mid-1);
quick_sort(v,mid+1,high);
}
}
};
leetcode每日一题第五十三天
最新推荐文章于 2024-10-31 16:16:13 发布