/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
int hash[1001],i=0,j=0;
int* ret=(int*)malloc(1001*sizeof(int));
memset(hash,0,sizeof(hash));
memset(ret,0,sizeof(ret));
*returnSize=0;
while(i<nums1Size)
{
if(!hash[nums1[i]])
hash[nums1[i]]++;
i++;
}
while(j<nums2Size)
{
if(hash[nums2[j]])
{
hash[nums2[j]]--;
ret[(*returnSize)++]=nums2[j];
}
j++;
}
return ret;
}
两个数组的交集(力扣
最新推荐文章于 2024-05-14 20:06:41 发布