18-m-4Sum

来源:互联网 发布:域名商 编辑:程序博客网 时间:2024/05/22 21:59

基本思路跟3sum一样,排序后两边夹,多了一层循环。但多了一层循环,就多了好多判断,尤其是有相同数字时要跳过已成为解的数字组合,同时也要注意要组合到所有数字。提交了不少次,都是要么少解要么重复解,一个个改进后最后AC。不得不再次赞一下leet的测试用例,确实把编写过程中我出现的细微bug都测出来了,当然这也说明自己还需要继续提高。另外这可能是为数不多n³时间度的题目吧,原以为会超时的。

如下:

void SortThreeSum(int num[], int n, int l, int h){    if (l >= h)        return;    int low = l, high = h;    int temp = num[low];    while (low < high) {        while (temp < num[high] && low < high)            high--;        num[low] = num[high];        while (temp >= num[low] && low < high)            low++;        num[high] = num[low];    }    num[low] = temp;    SortThreeSum(num, n, l, low - 1);    SortThreeSum(num, n, low + 1, h);}int** fourSum(int* nums, int numsSize, int target, int* returnSize) {    if (nums == NULL || numsSize < 4)        return NULL;    int **result = NULL;    int size = 1;    result = (int **)malloc(sizeof(int *) * size);    for (int i = 0; i < size; i++) {        result[i] = (int *)malloc(sizeof(int) * 4);        memset(result[i], 0, sizeof(int));    }    int rIndex = 0;        SortThreeSum(nums, numsSize, 0, numsSize - 1); //借用写好的排序        int tempT = 0, low = 0, high = 0;    for (int i = 0; i < numsSize - 3; i++) { //[-5,-5,-3,-1,0,2,4,5], -7 //[-5,-3,-1,0,5], 1 //[-3,-3,-2,-2,-2,0,0,4], -1        for (int j = i + 1; j < numsSize - 1; j++) {            tempT = target - (nums[i] + nums[j]);            low = j + 1, high = numsSize - 1;            while (low < high) {//                while (nums[low] + nums[high] < tempT && low < high) //递进太快,可能会跳过low中与high的组合//                    low++;//                while (nums[low] + nums[high] > tempT && low < high)//                    high--;                if (nums[low] + nums[high] < tempT) //不用while用if确保循环能比到每一个low+high的组合                    low++;                else if (nums[low] + nums[high] > tempT)                    high--;                if (low == high)                    break;                if (nums[low] + nums[high] == tempT) { //[-10,-8,-5,2], -21                    result[rIndex][0] = nums[i];                    result[rIndex][1] = nums[j];                    result[rIndex][2] = nums[low];                    result[rIndex][3] = nums[high];                    rIndex++;                    *returnSize += 1;                    result = (int **)realloc(result, sizeof(int *) * (*returnSize + 1));                    result[*returnSize] = (int *)malloc(sizeof(int) * 4);                    memset(result[*returnSize], 0, sizeof(int) * 4);                    if (nums[low] == nums[low + 1]) {                        while (nums[low] == nums[low + 1] && low < high)                            low++;                    }                    if (nums[high] == nums[high - 1]) {                        while (nums[high] == nums[high - 1] && high > low)                            high--;                    }                    low++, high--;                }            }            if (nums[j] == nums[j + 1]) {                while (nums[j] == nums[j + 1])                    j++;            }        }        if (nums[i] == nums[i + 1]) {            while (nums[i] == nums[i + 1])                i++;        }    }        return result;}


0 0