从数组中选出n个数之和为k

LeetCode15. 3Sum

题目描述:

https://leetcode.com/problems/3sum/

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:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
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)

LeetCode15. 3Sum 相当于1. Two Sum的一个变形嘛,题目大意就是从无序数组中找3个数a,b,c,使得a+b+c=0。返回数组中所有的可能作为结果集,结果集中不能包含重复。

思路:

我们可以写成a+b=-c的形式,-c作为target,只不过这次target不是传参指定,而是从给定的数组中找。
遍历i=0到i=len-2,使得target=-nums[i],然后再用双指针的方法选数,选数的范围[i+1,len-1]。
唯一需要注意的就是去重。

代码:
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int len=nums.size();
        vector<vector<int>> res;
        if(len==0){
            return res;
        }
        sort(nums.begin(),nums.end());
        int low,high,target;
        for(int i=0;i<len-1;i++){
            if(i>0 && nums[i-1]==nums[i]){
                continue;
            }
            target=0-nums[i];
            low=i+1;
            high=len-1;
            while(low<high){
                if(nums[low]+nums[high]==target){
                    vector<int> v;
                    v.push_back(nums[i]);
                    v.push_back(nums[low]);
                    v.push_back(nums[high]);
                    res.push_back(v);
                    low++;
                    high--;
                    while(low<high && nums[low-1]==nums[low]){
                        low++;
                    }
                    while(low<high && nums[high+1]==nums[high]){
                        high--;
                    }
                }
                else if(nums[low]+nums[high]<target){
                    low++;
                }
                else{
                    high--;
                }
            }
        }
        return res;
    }
};

题目2

如果是从数组中选出n个数之和为k呢,返回结果集,结果集不含重复。

思路:

我们就用递归回溯来解决问题了。
需要注意的还是去重。
我用pre来处理重复问题,对于递归进入的同一层函数,所选的数字不能相同,所以用pre来记录下标, 如果相同就continue.

代码:
void combinationSum(vector<vector<int>>& res, vector<int>& candidates, int len, int cur, int sum,int n, int target, vector<int>& v){
    if (sum > target || cur > len || v.size()>n){
        return;
    }
    else if (sum == target && v.size()==n){
        res.push_back(v);
        return;
    }
    else{
        int pre = -1;
        for (int i = cur+1; i < len; i++){
            if (pre != -1 && candidates[pre] == candidates[i]){
                continue;
            }
            pre = i;
            sum += candidates[i];
            v.push_back(candidates[i]);
            combinationSum(res, candidates, len, i + 1, sum,n, target, v);
            v.pop_back();
            sum -= candidates[i];
        }
    }
}
vector<vector<int>> combinationSum(vector<int>& candidates, int n, int target){
    int len = candidates.size();
    vector<vector<int>> res;
    if (len == 0){
        return res;
    }
    vector<int> v;
    sort(candidates.begin(),candidates.end());
    combinationSum(res, candidates, len, -1, 0,n, target, v);
    return res;
}

int main()
{
    vector<int> candidates;
    candidates.push_back(2);
    candidates.push_back(1);
    candidates.push_back(0);
    candidates.push_back(7);
    candidates.push_back(1);
    candidates.push_back(0);
    candidates.push_back(7);
    vector<vector<int>> res = combinationSum(candidates,2,9);
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值