本人本人用Devc++测试正确,IEETcode不对
这句话本人也不太明白 Note: The returned array must be malloced, assume caller calls free().
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1″s size is small compared to num2″s size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
void Quick_sort(int*k,int s,int t){
int i,j,temp;
if(s<t){
i=s;
j=t+1;
while(1){
do i++;
while(!(k[s]<=k[i]||i==t));
do j–;
while(!(k[s]>=k[j]||j==s));
if(i<j)
{temp=k[i];
k[i]=k[j];
k[j]=temp;}
else
break;
}
temp=k[s];
k[s]=k[j];
k[j]=temp;
Quick_sort(k,s,j-1);
Quick_sort(k,j+1,t);
}
}
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
int i=0,j=0,k=0;
Quick_sort(nums1,0,nums1Size-1);
Quick_sort(nums2,0,nums2Size-1);
while(i<nums1Size&&j<nums2Size){
while(nums1[i]<nums2[j])
{ i++;}
while(nums1[i]>nums2[j])
{ j++;}
while(nums1[i]==nums2[j]){
returnSize[k]=nums1[i];
k++;
i++;
j++;
}
}
这句话本人也不太明白 Note: The returned array must be malloced, assume caller calls free().
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1″s size is small compared to num2″s size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
void Quick_sort(int*k,int s,int t){
int i,j,temp;
if(s<t){
i=s;
j=t+1;
while(1){
do i++;
while(!(k[s]<=k[i]||i==t));
do j–;
while(!(k[s]>=k[j]||j==s));
if(i<j)
{temp=k[i];
k[i]=k[j];
k[j]=temp;}
else
break;
}
temp=k[s];
k[s]=k[j];
k[j]=temp;
Quick_sort(k,s,j-1);
Quick_sort(k,j+1,t);
}
}
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
int i=0,j=0,k=0;
Quick_sort(nums1,0,nums1Size-1);
Quick_sort(nums2,0,nums2Size-1);
while(i<nums1Size&&j<nums2Size){
while(nums1[i]<nums2[j])
{ i++;}
while(nums1[i]>nums2[j])
{ j++;}
while(nums1[i]==nums2[j]){
returnSize[k]=nums1[i];
k++;
i++;
j++;
}
}
return(returnSize);
}
解决方案
20
Visual C++ 2010 Express简体中文版http://pan.baidu.com/s/1bnwRVLt
参考
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c
参考
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c