/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int min(int i,int j){
if(i<j) return i;
return j;
}
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
int hash1[1001]={0};
int hash2[1001]={0};
int* returnarray=(int*)malloc(sizeof(int)*1000);
for(int i=0;i<1000;i++) returnarray[i]=0;
for(int i=0;i<nums1Size;i++) hash1[nums1[i]]++;
for(int i=0;i<nums2Size;i++) hash2[nums2[i]]++;
int n=0;
for(int i=0;i<1001;i++){
if(hash1[i]!=0 && hash2[i]!=0){
int t=min(hash1[i],hash2[i]);
while(t!=0) {
returnarray[n++]=i;
t--;
}
}
}
*returnSize=n;
return returnarray;
}
16min
结果:
标签:Note,交集,II,int,数组,350 From: https://www.cnblogs.com/llllmz/p/18041712