llllmz

导航

349. 两个数组的交集C

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
    int hash1[1001]={0};
    int hash2[1001]={0};
    int* tem=(int*)malloc(sizeof(int)*1000);
    for(int i=0;i<1000;i++) tem[i]=0; 
    int n=0;
    for(int i=0;i<nums1Size;i++){
        hash1[nums1[i]]=1;
    }
    for(int i=0;i<nums2Size;i++){
        if(hash1[nums2[i]]==1 && hash2[nums2[i]]==0){
            tem[n]=nums2[i];
            n++;
        }
        hash2[nums2[i]]=1;
    }
    *returnSize=n;
    return tem;
}

结果:

用时 7分钟

posted on 2024-02-28 20:07  神奇的萝卜丝  阅读(17)  评论(0)    收藏  举报