LeetCode 350.两个数组的交集(用c语言hash表完成)
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
看到网上很多关于LeetCode的题,但是很少有用c去写。也许很多人都喜欢c++或者java,但我想不少用c的人为此苦恼,我刷个LeetCode还得学别的咯?
直接上代码
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct hashTable{
int key; //键值
int val; //数字出现的次数
UT_hash_handle hh;
};
int *returnNum(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize)
{
struct hashTable *set=NULL;
int *res=(int *)malloc(sizeof(int )*nums2Size);
int count =-1;
for(int i=0;i<nums2Size;i++)
{
struct hashTable *tmp;
HASH_FIND_INT(set,nums2+i,tmp);//函数库函数,用来查找
if(tmp==NULL)//如果没找到就将这个数加入到hash表
{
tmp=(struct hashTable*)malloc(sizeof(struct hashTable));
tmp->key=nums2[i];
tmp->val=1;
HASH_ADD_INT(set,key,tmp);//函数库函数,用来将没有的数添加进hash表中
}
else{//反之找到了就将相应的数+1
tmp->val++;
}
}
for(int j=0;j<nums1Size;j++)
{
struct hashTable *tmp;
HASH_FIND_INT(set,nums1+j,tmp);
if(tmp!=NULL&&tmp->val!=0)//处理次数不一致的情况,同时不再处理没找到的情况
{
tmp->val--;
count++;
res[count]=tmp->key;
}
}
*returnSize=++count;//因为count初始值问题,所以count要+1
return res;
}
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
struct hashTable *set=NULL;
int *res=(int *)malloc(sizeof(int )*nums2Size);
int count =-1;
if(nums1Size>nums2Size)
{
int *res= returnNum( nums1, nums1Size, nums2, nums2Size,returnSize);
return res;
}
else
{
int *res= returnNum( nums2, nums2Size, nums1, nums1Size,returnSize);
return res;
}
* returnSize=0;
return res;
}
大概思路
其实本质就是先遍历短的数组,再遍历长的数组,时间复杂度为O(m+n)
,遍历短的数组时把所有数加入到hash表(遇到相同数就加一),遍历长数组时就是一个查找过程了,找到就加入到输出数组中,同时hash表相应的数的次数减一。细心的人肯定发现了,我一开始动态申请的空间比较大,最后返回的数组应该是小于等于这个大小的,多少有点浪费空间的嫌疑。leetcode上不知道什么原因用realloc一直没成功,所以就用returnsize的机制保证返回数组大小正确了。
//细心的人肯定发现了,我的思路其实就是LeetCode的思路,只不过他没有c语言版而以