typedef struct node{
int sum;
int count;
struct node* repeatnext;
}hash;
void init_hash(hash* h){
for(int i=0;i<128;i++){
h[i].sum=0;
h[i].count=0;
h[i].repeatnext=NULL;
}
}
hash* find_hash(hash* h,int number){
int index=abs(number)%127;
hash* temp=&h[index];
while(temp){
if(temp->sum==number) return temp;
temp=temp->repeatnext;
}
return NULL;
}
void insert_hash(hash* h,int number){
int index=abs(number)%127;
if(h[index].count==0){
h[index].sum=number;
h[index].count=1;
}else{
if(find_hash(h,number)){
find_hash(h,number)->count++;
}else{
hash* temp=(hash*)malloc(sizeof(hash));
temp->sum=number;
temp->count=1;
temp->repeatnext=NULL;
temp->repeatnext=h[index].repeatnext;
h[index].repeatnext=temp;
}
}
}
int fourSumCount(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums3, int nums3Size, int* nums4, int nums4Size){
hash h[128];
init_hash(h);
int n=nums1Size;
int sumcount=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
insert_hash(h,nums1[i]+nums2[j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int temp=nums3[i]+nums4[j];
if(find_hash(h,0-temp)){
sumcount+=find_hash(h,0-temp)->count;
}
}
}
return sumcount;
}
结果:
标签:repeatnext,index,四数,hash,temp,int,454,number,II From: https://www.cnblogs.com/llllmz/p/18047134