有些测试正确 有些不正确

sky?? 2016-06-04 05:54:26
我自己用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++;
}
}

return(returnSize);

}
...全文
159 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-06-06
  • 打赏
  • 举报
回复
Visual C++ 2010 Express简体中文版http://pan.baidu.com/s/1bnwRVLt 参考 C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧