3ms C解决方案


  • 0
    L
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
        int *arr=(int*)malloc(sizeof(int)*(nums1Size+10));
        int *nums3=(int *)malloc(sizeof(int)*1000);
        for(int i=0;i<1000;i++)
        nums3[i]=0;
        int j=0;
         for(int i=0;i<nums1Size;i++)
            nums3[nums1[i]]++;
         
         for(int i=0;i<nums2Size;i++)
         {
             if(nums3[nums2[i]]!=0)
             {
                 arr[j++]=nums2[i];
                 nums3[nums2[i]]=0;
             }
         }
         free(nums3);
         *returnSize=j;
         return arr;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.