三数之和

 

Given an array nums of n integers, are there elements abc in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
int cmp(const void *a,const void *b){
    return (*((int *)a))-*((int *)b);
}
int** threeSum(int* nums, int numsSize, int* returnSize) {
    if(nums==NULL||numsSize<3){
        return NULL;
    }
    //int nn=(numsSize)*(numsSize-1)*(numsSize-2)/6;
    //int **result=(int **)malloc((nn)*sizeof(int *));
    int **result=(int **)malloc(30000*sizeof(int *));
    //先对nums进行排序
    qsort(nums,numsSize,sizeof(nums[0]),cmp);
    int index=0;
    for(int i=0;i<numsSize;i++){
        int begin=i+1;
        int end=numsSize-1;

        while(begin<end){//借助于2Sum的方法
            int dif=nums[i]+nums[begin]+nums[end];
            if(dif==0){//找到精确的组合,则进行保存
                     result[index]=(int *)malloc(3*sizeof(int));
                    if(result[index]==NULL){
                        exit(EXIT_FAILURE);
                    }
                    //将这三个数进行排序
                    result[index][0]=nums[i];
                    result[index][1]=nums[begin];
                    result[index][2]=nums[end];
                    //检查下result有没有重复的
                    for(int i1=0;i1<index;i1++) {
                        if(result[i1][0]==result[index][0]&&result[i1][1]==result[index][1]&&result[i1][2]==result[index][2]){
                            free(result[index]);
                            result[index]=NULL;
                            index--;
                        }
                    }
                    index++;
                    begin++;//

            } //通过dif的符号在改变begin和end,使结果更加逼近与target
            else if(dif<0){
                    begin++;
                }
            else{//decrease value
                    end--;
            }
        }


    }
    *returnSize=index;
    return result;
}
int cmpfunc(const void * a, const void * b)
{
	return (*(int*)a - *(int*)b);
}

int** threeSum(int* nums, int numsSize, int* returnSize)
{
	int front, back;
	int i = 0;
	int **result = 0;
	int s = 0;

	qsort(nums, numsSize, sizeof(int), cmpfunc);

	while (i < numsSize)
	{
		int target = -nums[i];
		if (target < 0)
		{
			break;
		}

		front = i + 1;
		back = numsSize - 1;

		while (front < back)
		{
			int sum = nums[front] + nums[back];
			if (sum < target)
			{
				front++;
			}
			else if (sum > target)
			{
				back--;
			}
			else
			{
				s++;
				result = (int **)realloc(result, sizeof(int *)*s);
				int *temp = (int *)malloc(sizeof(int) * 3);
				temp[0] = nums[i];
				temp[1] = nums[front];
				temp[2] = nums[back];
				result[s-1] = temp;

				while (front < back && nums[front] == temp[1])
				{
					front++;
				}
				while (front < back && nums[back] == temp[2])
				{
					back--;
				}
			}
		}
		while ((i + 1) < numsSize && nums[i] == nums[i + 1])
		{
			i++;
		}
		i++;
	}

	//returnSize = (int *)malloc(sizeof(int));
	*returnSize = s;
	return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值