Combination Sum

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

The same repeated number may be chosen from C unlimited number of times.

Note:

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

For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]

 

简单的回溯法(递归实现).

比如对于数组3,2,6,7,target = 7,对数组排序得到[2,3,6,7]

1、第1个数字选取2, 那么接下来就是解决从数组[2,3,6,7]选择数字且target = 7-2 = 5

2、第2个数字选择2,那么接下来就是解决从数组[2,3,6,7]选择数字且target = 5-2 = 3

3、第3个数字选择2,那么接下来就是解决从数组[2,3,6,7]选择数字且target = 3-2 = 1

4、此时target = 1小于数组中的所有数字,失败,回溯,重新选择第3个数字

5、第3个数字选择3,那么接下来就是解决从数组[2,3,6,7]选择数字且target = 3-3 = 0

6、target = 0,找到了一组解,继续回溯寻找其他解

 

需要注意的是:如果数组中包含重复元素,我们要忽略(因为每个数字可以选择多次,如果不忽略的话,就会产生重复的结果)。貌似oj的测试集数组中都不包含重复的数字

 

  1. class Solution {
  2. private:
  3. vector<vector<int> > res;
  4. public:
  5. vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
  6. sort(candidates.begin(), candidates.end());//为了输出结果递增,因此先对数组排序
  7. vector<int> tmpres;
  8. helper(candidates, 0, target, tmpres);
  9. return res;
  10. }
  11.  
  12. //从数组candidates[index,...]寻找和为target的组合
  13. void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres)
  14. {
  15. if(target == 0)
  16. {
  17. res.push_back(tmpres);
  18. return;
  19. }
  20. for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
  21. if(i == 0 || candidates[i] != candidates[i-1])//由于每个数可以选取多次,因此数组中重复的数就不用考虑
  22. {
  23. tmpres.push_back(candidates[i]);
  24. helper(candidates, i, target - candidates[i], tmpres);
  25. tmpres.pop_back();
  26. }
  27. }
  28. };

 


Combination Sum II

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 (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ 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]

 

和上一题差不多,只是每个元素只能选一次。

由于有重复元素的存在,比如数组为[1(1),1(2),2,3],target = 6. 可能出现重复结果1(1),2,3 和 1(2),2,3                          本文地址

我们可以如下处理:如果数组中当前的数字出现重复,在前面重复了k次,且临时结果数组中也包含了k个当前数字,那么当前的数字可以选择;否则就不选择当前数字

  1. class Solution {
  2. private:
  3. vector<vector<int> >res;
  4. public:
  5. vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
  6. sort(candidates.begin(), candidates.end());
  7. vector<int> tmpres;
  8. helper(candidates, 0, target, tmpres, 0);
  9. return res;
  10. }
  11.  
  12. //从数组candidates[index,...]寻找和为target的组合,times为前一个数字candidates[index-1]重复出现的次数
  13. void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres, int times)
  14. {
  15. if(target == 0)
  16. {
  17. res.push_back(tmpres);
  18. return;
  19. }
  20. for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
  21. {
  22. if(i > 0 && candidates[i] == candidates[i-1])times++;
  23. else times = 1;
  24. if(times == 1 || (tmpres.size() >= times-1 && tmpres[tmpres.size()-times+1] == candidates[i]))
  25. {
  26. tmpres.push_back(candidates[i]);
  27. helper(candidates, i+1, target - candidates[i], tmpres, times);
  28. tmpres.pop_back();
  29. }
  30. }
  31. }
  32. };

 

还有一种方法是,在每个子问题的数组中,重复的数字都不选择,这种更简洁

  1. class Solution {
  2. private:
  3. vector<vector<int> >res;
  4. public:
  5. vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
  6. sort(candidates.begin(), candidates.end());
  7. vector<int> tmpres;
  8. helper(candidates, 0, target, tmpres);
  9. return res;
  10. }
  11.  
  12. //从数组candidates[index,...]寻找和为target的组合
  13. void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres)
  14. {
  15. if(target == 0)
  16. {
  17. res.push_back(tmpres);
  18. return;
  19. }
  20. for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
  21. {
  22. if(i > index && candidates[i] == candidates[i-1])continue;//当前子问题中,重复数字都不选择
  23. tmpres.push_back(candidates[i]);
  24. helper(candidates, i+1, target - candidates[i], tmpres);
  25. tmpres.pop_back();
  26. }
  27. }
  28. };

 

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3802647.html

LeetCode:Combination Sum I II的更多相关文章

  1. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  2. [LeetCode] Combination Sum II 组合之和之二

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

  3. Leetcode 39 40 216 Combination Sum I II III

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  4. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  5. 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

    引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...

  6. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

随机推荐

  1. AT91-PWM应用

    步骤1: make menuconfig配置内核, 开启PWM输出功能. Device Drivers ---> Misc devices  ---> <*>Atmel AT3 ...

  2. [Linux]服务管理:rpm包, 源码包

    --------------------------------------------------------------------------------------------------- ...

  3. LeetCode(76) Minimum Window Substring

    题目 Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  4. homework 15 2016 6 2 模板

    #include<iostream>#include<cmath>#include<cstdio> using namespace std; template &l ...

  5. javaBean与Map<String,Object>互转

    背景:有时候想不通阿帕奇的BeanUtils是怎么进行map和Bean互相转化的. 工作闲暇之余,自己写个一小段代码,一探究竟,试试才发现,原来一切并非我们想的那么什么和复杂. 注:这里只是简单实例, ...

  6. 关于 pgsql 数据库json几个函数用法的效率测试

    关于 pgsql 数据库json几个函数用法的效率测试 关于pgsql 几个操作符的效率测试比较1. json::->> 和 ->> 测试方法:单次运行100次,运行10个单次 ...

  7. h5专题应该兼容那些浏览器?

    本人做专题还不算很多,但是也很腻烦了.一般一个专题制作也就3天,可是调试得4/5天.除了销售客户各种无休止的改改改.还有一点很重要就是浏览器的兼容性.刚开始做专题的时候天真的以为苹果只要兼容到ipho ...

  8. SQL语法的重要知识点总结

    好几年没写SQL语句了.现在到了新的team,需要用到数据库.作为QA的话时常需要使用客户端工具连接到数据库中找寻或修改数据.这么长时间没使用,一些SQL的使用技巧都忘得差不多了.晚上看了一些资料,花 ...

  9. 《介绍一款开源的类Excel电子表格软件》续:七牛云存储实战(C#)

    两个月前的发布的博客<介绍一款开源的类Excel电子表格软件>引起了热议:在博客园有近2000个View.超过20个评论. 同时有热心读者电话咨询如何能够在SpreadDesing中实现存 ...

  10. c#访问http接口的"编码"问题

    记一次访问http数据接口的爬坑经历,一般访问一个http接口. 无非就是这么几行代码: HttpWebRequest request = (HttpWebRequest)WebRequest.Cre ...