15.3Sum

description:

 15
3Sum   
 21.8%Medium

Given an array S of n integers, are there elements a, b, c in S 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.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

my solution:

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        std::sort(nums.begin(), nums.end());
        std::vector<vector<int>> result;
        int tempSum = 0, twoSum = 0;
        int newBegin = 0;
        int newEnd = nums.size() - 1;
        for (int i = 0; i < nums.size(); ++i)
        {
        	twoSum = -nums[i];
        	newBegin = i + 1;
        	newEnd = nums.size() - 1;

        	while(newBegin < newEnd) {
        		tempSum = nums[newBegin] + nums[newEnd];
        		if(tempSum < twoSum) {
        			newBegin++;
        		} else if(tempSum > twoSum) {
        			newEnd--;
        		} else {
        			std::vector<int> v;
        			v.push_back(nums[i]);
        			v.push_back(nums[newBegin]);
        			v.push_back(nums[newEnd]);
        			result.push_back(v);
        			while (newBegin < newEnd && nums[newBegin] == v[1]) newBegin++;
                	while (newBegin < newEnd && nums[newEnd] == v[2]) newEnd--;
        		}
        	}
            while (i < nums.size() && nums[i + 1] == nums[i]) i++;
        	if(nums[i] > 0) break;
        }
        return result;
    }
};

thought:

这道题的思路就是把三个数相加转化成两个数相加。

首先,对数组进行排序,其次从头到尾遍历数组,对每个数字取反都当成所求两个数字的和进行处理。

然后以该数字为界限,向后寻找两个数字,使之相加为该数字的负值。其中为了减小开销,两数字分别从头和尾部找起,这样做能把2维问题转化成1维问题。

最后的优化就是,跳过相同的数字,同时当遍历的数字大于0时,结束搜索,因为大于0时,剩下两个数字皆大于0,所以不用继续了。











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值