2024年6月8日 星期六

[350] Intersection of Two Arrays II

嗯?! 明明上周才寫過前身,但已經失憶 TOT
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
int *hash = calloc (1001, sizeof(int));
for (int i=0; i<nums1Size; i++)
hash[nums1[i]]++;
int *ret = calloc (nums1Size, sizeof(int));
int idx = 0;
for (int i=0; i<nums2Size; i++)
{
if (hash[nums2[i]]>0)
{
hash[nums2[i]]--;
ret[idx++]= nums2[i];
}
}
*returnSize = idx;
return ret;
}

沒有留言:

張貼留言