我知道这是解决3sum问题的一种暴力和非最优方法。它做的工作是检测三胞胎的总和为零。但我正在寻找检测重复三胞胎的方法我无法想出逻辑来寻找已经被考虑过的类似的三元组组合。谢谢!

int** threeSum(int* nums, int numsSize, int *returnSize)
{
int count = 0;
int **res = NULL;
for (int i=0; i<(numsSize-2) ; i++)
{
    for (int j=i+1; j<(numsSize-1) ; j++)
    {
        for(int k=j+1; k<numsSize ; k++)
        {
            if (0==(nums[i]+nums[j]+nums[k]))
            {
                if (count > 0)
                {
                    res = (int **)realloc(res,sizeof(int *)*(count+1));
                    res[count] = (int *) malloc(sizeof(int)*3);
                    res[count][0] = nums[i];
                    res[count][1] = nums[j];
                    res[count][2] = nums[k];
                    count++;
                }
                else if (0==count)
                {
                    res = (int **) malloc(sizeof(int *)*1);
                    res[0] = (int *) malloc(sizeof(int)*3);
                    res[0][0] = nums[i];
                    res[0][1] = nums[j];
                    res[0][2] = nums[k];
                    count++;
                }
            }
        }
    }
}
*returnSize=count;
if (count > 0)
return res;
else
return NULL;``
}

最佳答案

由于存在冗余块,因此可以简化代码:
realloc可以用NULL调用,其行为类似于malloc
res被初始化为count时,返回0是正常的。
您可以检查重复项,可以对三元组进行排序,并在插入前在匹配项列表中搜索它,还可以使用蛮力:

int **threeSum(int *nums, int numsSize, int *returnSize) {
    int count = 0;
    int **res = NULL;
    for (int i = 0; i < numsSize - 2; i++) {
        for (int j = i + 1; j < numsSize - 1; j++) {
            for (int k = j + 1; k < numsSize; k++) {
                if (nums[i] + nums[j] + nums[k] == 0) {
                    int a = nums[i], b = nums[j], c = nums[k], n;
                    if (a > b) { int t = a; a = b; b = t; }
                    if (b > c) { int t = b; b = c; c = t; }
                    if (a > b) { int t = a; a = b; b = t; }
                    for (n = 0; n < count; n++) {
                        if (a == res[n][0] && b == res[n][1])
                            break;
                    }
                    if (n == count) {
                        int **p = realloc(res, sizeof(*p) * (count + 1));
                        int *p1 = malloc(sizeof(*p1) * 3);
                        if (p == NULL || p1 == NULL) {
                            for (n = 0; n < count; n++)
                                free(res[n]);
                            free(res);
                            free(p);
                            free(p1);
                            *returnSize = -1;
                            return NULL;
                        }
                        res = p;
                        p1[0] = a;
                        p1[1] = b;
                        p1[2] = c;
                        res[count] = p1;
                        count++;
                    }
                    break; // any other match would be a duplicate.
                }
            }
        }
    }
    *returnSize = count;
    return res;
}

笔记:
不需要测试是否NULL,因为c == res[n][2]只包含匹配项
在找到匹配后,无需再测试相同的resi元素。

10-08 11:23