15. 三数之和

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请

你返回所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。
示例 2:

输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。
示例 3:

输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/3sum

int cmp(void *a,void *b)
{
    return *((int *)a) - *((int *)b);
}

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    int **ret = NULL;
    int count = 0;
    int *retcol = NULL;
    (*returnSize) = count;
    (*returnColumnSizes) = retcol;

    if (!nums || numsSize <= 2)
    {
        return ret;
    }

	qsort(nums,numsSize,sizeof(int),(void *)cmp);

	ret = (int **)realloc(ret,sizeof(int *)*numsSize*numsSize);
    retcol = (int *)realloc(retcol,sizeof(int)*numsSize*numsSize);

    for (int i = 0; i < numsSize-2;++i)
    {
        if (nums[i] > 0)
        {
            break;
        }//因为数组已经经过冒泡排序处理,所以如果三个数中的最小值大于0,则和一定不符合条件
        if (i > 0 && nums[i] == nums[i-1] )//来到一组相同数字
        {
            continue;
        }
        int head = i+1;
        int tail = numsSize-1;
        while (head < tail)
        {
            int target = nums[head] + nums[tail] + nums[i];
            if (target == 0)
            {
                #if 0
                在内部逐步扩展指针数组,会导致超时!
                ret = (int **)realloc(ret,sizeof(int *)*(count+1));
                retcol = (int *)realloc(retcol,sizeof(int)*(count+1));
                #endif
                retcol[count] = 3;
                ret[count] = (int *)malloc(sizeof(int)*3);
                ret[count][0] = nums[i];
                ret[count][1] = nums[head];
                ret[count][2] = nums[tail];

                ++count;

                while (head < tail && nums[head] == nums[head+1])
                {
                    ++head;
                }//当数字相同时,头向右移动

                while (head < tail && nums[tail] == nums[tail - 1])
                {
                    --tail;
                }//当数字相同时,尾向左移动

                ++head;
                --tail;
            }
            else if (target < 0)
            {
                ++head;
            }
            else
            {
                --tail;
            }//三数和不匹配时候移动头和尾
        }
    }

    (*returnSize) = count;
    (*returnColumnSizes) = retcol;

    return ret;
}

https://blog.csdn.net/Cooler_z/article/details/122384559

qsort是stdlib.h中的函数,因此使用前需要声明:#include<stdlib.h>
通过定义一个函数cmp,通过cmp返回的参数来确定排序规则,需要注意的是:cmp函数的参数需要以const void a,const void b的形式来定义,表示a和b的类型是未确定的,在return中进行强制类型转换为int型。(int)a-(int)b表示以递增顺序,若想以递减只需将a和b换位。

对于不同类型的数组排序,qsort函数的格式都是相同的,唯一不同在于cmp函数中的返回值类型。

posted @ 2023-07-09 09:46  noobwei  阅读(6)  评论(0编辑  收藏  举报