《leetCode》:4Sum

题目

Given an array S of n integers, are there elements a, b, c, and d in S
 such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. 
(ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
    For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

    A solution set is:
    (-1,  0, 0, 1)
    (-2, -1, 1, 2)
    (-2,  0, 0, 2)

实现代码如下:

//暴力搜索不知道能不能过,在这里我也不尝试了
//寻找更好的方法:还是按照2Sum来做
#include<stdio.h>
#include<stdlib.h>
#define N 4
int cmp(const void *a,const void *b){
    return (*((int *)a))-(*((int *)b));
}
int** fourSum(int* nums, int numsSize, int target, int* returnSize) {
    if(nums==NULL||numsSize<4){
        return NULL;
    }
    int nn=(numsSize)*(numsSize-1)*(numsSize-2)*(numsSize-3)/24;//即最多可能种情况。 
    int **result=(int **)malloc(nn*sizeof(int *));
    if(result==NULL)
        exit(EXIT_FAILURE);
    int index=0;
    //先进行排序
    qsort(nums,numsSize,sizeof(nums[0]),cmp); 

    for(int i=0;i<numsSize-3;i++){
        for(int j=i+1;j<numsSize-2;j++){
            int begin=j+1;
            int end=numsSize-1;
            while(begin<end){
                int diff=nums[i]+nums[j]+nums[begin]+nums[end]-target;
                if(diff==0){
                    //保存结果 
                    //检查下是否出现了重复的。
                    bool flag=true;//用来标识是否出现了重复的 
                    for(int k=0;k<index;k++){
                        if(result[k][0]==nums[i]&&result[k][1]==nums[j]&&result[k][2]==nums[begin]&&result[k][3]==nums[end]){
                            flag=false;
                            begin++;//这里要注意,要进行加 1操作。 
                            break;
                        }
                    } 
                    if(flag){
                        result[index]=(int *)malloc(N*sizeof(int));
                        if(result[index]==NULL)
                            exit(EXIT_FAILURE);
                        result[index][0]=nums[i];
                        result[index][1]=nums[j];
                        result[index][2]=nums[begin];
                        result[index][3]=nums[end];
                        index++;
                        begin++;
                    }

                }
                else if(diff<0){
                    begin++;
                } 
                else{
                    end--;
                }
            }

        }
    }
    *returnSize=index;
    return result;
}
//测试代码
int main(void){
    int n;
    int target;
    while(scanf("%d %d",&n,&target)!=EOF&&n>=4){
        int *arr=(int *)malloc(n*sizeof(int ));
        if(arr==NULL){
            exit(EXIT_FAILURE);
        }
        for(int i=0;i<n;i++){
            scanf("%d",arr+i);
        }
        int returnSize=0;
        int **result=fourSum(arr,n,target,&returnSize);
        for(int i=0;i<returnSize;i++){
            int *temp=(result[i]);
            for(int j=0;j<N;j++){
                printf("%d ",temp[j]);
            }
            printf("\n");
        }

        for(int i=0;i<returnSize;i++){
            free(result[i]);
            result[i]=NULL;
        }

    }
    return 0;
}

上面的代码运行题目给出的例子是能够运行的,但是提交是报Runtime error,具体错误如下,
《leetCode》:4Sum_第1张图片

由于代码的思路比较简单,由于做过2Sum/3Sum之后,对这种思路真是太熟悉了,反复检查了每行代码,反复检查,但是就是报错,无奈之下,只有仔细研究下,报错的这个测试用例:

int target=-236727523;
    int arr[200]={91277418,66271374,38763793,4092006,11415077,60468277,1122637,72398035,
    -62267800,22082642,60359529,-16540633,789394,18318594,-3246760,-44346548,     -21370279,42493875,25185969,83216261,-70078020,-53687927,-76072023,           -48859327,-2183204,17736781,31189878,-23814871,-3588016         6,39204002,93248899,-42067196,-49473145,-75235452,-61923200,64824322,   
          -88279865,53092631,75125438,44270568,-23129316,-846252,-59608044,90938699,80923976,3534451,6218186,41256179,-9165388,-11897463,           39520751,-90474508,-27860023,65164540,26582346,-20183515,99018741,-2826130,-28461563,-24759460,-83828963,-1739800,71207113,26434787,  
                  52931083,-33111208,38314304,-29429107,-5567826,-5149750,9582750,85289753,75490866,-93202942,-85974081,7365682,-42953023,21825824, 
          96928957,44328607,49293516,-39034828,5945763,-47046163,10986423,63478877,30677010,-21202664,-86235407,3164123,8956697,-9003909, -18929014,-73824245};

此测试用例的大小为200个,看到这个大小之后,我才反应过来是这行代码导致的错误:由于numsSize过大,导致分配内存失败。因此就不能AC

    int nn=(numsSize)*(numsSize-1)*(numsSize-2)*(numsSize-3)/24;//即最多可能种情况。 
    int **result=(int **)malloc(nn*sizeof(int *));
    if(result==NULL)
        exit(EXIT_FAILURE);

解决方法:将int nn=(numsSize)*(numsSize-1)*(numsSize-2)*(numsSize-3)/24;改为

int nn=(numsSize)*(numsSize-1);

改为之后,就AC了,AC结果如下:
《leetCode》:4Sum_第2张图片

小结

内存这个东西是性能的瓶颈。在这个题中,解决这个问题花了一定的时间。

你可能感兴趣的:(LeetCode,C语言,4sum)