int cmp(const void*a,const void*b){
return *(int *)a-*(int *)b;
}
int** fourSum(int* nums, int numsSize, int target, int* returnSize) {
if(numsSize<4)return 0;
qsort(nums,numsSize,sizeof(int),cmp);
int **foursum=(int**)malloc(200*sizeof(int*));
int a,b,c,d;
*returnSize=0;
for(a=0;a<numsSize-3;a++){
//int sum=nums[a]+nums[a+1]+nums[a+2]+nums[a+3];
if(nums[a]+nums[a+1]+nums[a+2]+nums[a+3]>target)
break;
if(nums[a]+nums[numsSize-1]+nums[numsSize-2]+nums[numsSize-3]<target)
continue;
for(b=a+1;b<numsSize-2;b++){
if(nums[a]+nums[b]+nums[b+1]+nums[b+2]>target)
break;
if(nums[a]+nums[b]+nums[numsSize-1]+nums[numsSize-2]<target)
continue;
c=b+1;
d=numsSize-1;
while(c<d){
if(nums[a]+nums[b]+nums[c]+nums[d]>target)
d--;
else if(nums[a]+nums[b]+nums[c]+nums[d]<target)
c++;
else{
foursum[(*returnSize)++]=(int*)malloc(4*sizeof(int));
foursum[(*returnSize)-1][0]=nums[a];
foursum[(*returnSize)-1][1]=nums[b];
foursum[(*returnSize)-1][2]=nums[c];
foursum[(*returnSize)-1][3]=nums[d];
c++;
d--;
while(c<d&&nums[c]==nums[c-1])c++;
while(c<d&&nums[d]==nums[d+1])d--;
}
}
while(b<numsSize-2&&nums[b+1]==nums[b])
b++;
}
while(a<numsSize-1&&nums[a+1]==nums[a])
a++;
}
return foursum;
}
12-26
2895
11-03