[LeetCode]40.Combination Sum II

160 篇文章 28 订阅
【题目】

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 

【分析】

基本思路是先排好序,然后每次递归中把剩下的元素一一加到结果集合中,并且把目标减去加入的元素,然后把剩下元素(包括当前加入的元素)放到下一层递归中解决子问题。以start记录我们选到了第几个值,并且一直往后选,这样可以避免选到重复的子集。

这一题和上一题:[LeetCode]39.Combination Sum 差不多一样的思路,只要稍作修改就行。只要在for循环中加一个判断条件即可:

if(i > start && candidates[i] == candidates[i-1]){
continue;
}//if

这个判断的目的是排除同一层次相同元素的出现。例如:下面例题中有两个1,在第一次递归中不能都出现1,可以的是第一次递归出现1,第二次递归也可以出现一个1

【代码】

/*********************************
*   日期:2015-01-27
*   作者:SJF0115
*   题目: 40.Combination Sum II
*   网址:https://oj.leetcode.com/problems/combination-sum-ii/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
        // 中间结果
        vector<int> path;
        // 最终结果
        vector<vector<int> > result;
        int size = candidates.size();
        if(size <= 0){
            return result;
        }//if
        // 排序
        sort(candidates.begin(),candidates.end());
        // 递归
        DFS(candidates,target,0,path,result);
        return result;
    }
private:
    void DFS(vector<int> &candidates, int target,int start,vector<int> &path,vector<vector<int> > &result){
        int len = candidates.size();
        // 找到一组组合和为target
        if(target == 0){
            result.push_back(path);
            return;
        }//if
        for(int i = start;i < len;++i){
            // 同一层次不能出现相同元素
            if(i > start && candidates[i] == candidates[i-1]){
                continue;
            }//if
            // 剪枝
            if(target < candidates[i]){
                return;
            }//if
            path.push_back(candidates[i]);
            DFS(candidates,target-candidates[i],i+1,path,result);
            path.pop_back();
        }//for
    }
};

int main(){
    Solution solution;
    int target = 8;
    vector<int> vec;
    vec.push_back(10);
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(7);
    vec.push_back(6);
    vec.push_back(1);
    vec.push_back(5);
    vector<vector<int> > result = solution.combinationSum2(vec,target);
    // 输出
    for(int i = 0;i < result.size();++i){
        for(int j = 0;j < result[i].size();++j){
            cout<<result[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值