class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
sort(num.begin(), num.end()); vector<vector<int> > tmp;
vector<int> sel;
dfs(num, , target, sel, tmp);
return tmp;
} void dfs(vector<int> &num, int pos, int target, vector<int>& sel, vector<vector<int> >& res) {
if (target == ) {
res.push_back(sel);
return;
}
if (pos >= num.size()) return;
int cur = num[pos];
int dup = ;
int i = pos;
while (++i < num.size() && num[i] == cur) dup++; int add = ; for (i = ; (i <= dup + ) && add <= target; i++, add+=cur) {
if (i != ) {
sel.push_back(cur);
}
dfs(num, pos + dup + , target - add, sel, res);
}
for (i = add/cur - ; i>; i--) sel.pop_back();
} };

类似01背包,不过这是求和,虽然说每个元素最多用一次,但是从例子中发现,所给的候选数时会有重复的。因为输出要求不能有重复,这时我们可以把多个重复的候选数看成是同一个数取[0, k]次(k为该数出现的次数)这和最初的CombinationSum中类似(只不过这里指定了每个元素出现的次数,而不是原先的可以取无限次),这相当于把重复的候选数压到了一个位置上,当我们在该位置做出取(一次性取n,n<=k)和不取两种选择后,后面再也不会对该数考虑相同的问题,这使得我们可以避免输出雷同解。

LeetCode CombinationSum II的更多相关文章

  1. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  2. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. leetcode — combination-sum

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  4. LeetCode Permutaions II

    LeetCode解题之Permutaions II 原题 输出一个有反复数字的数组的全排列. 注意点: 反复数字的可能导致反复的排列 样例: 输入: nums = [1, 2, 1] 输出: [[1, ...

  5. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  6. [LeetCode] H-Index II 求H指数之二

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  7. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  8. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  9. LeetCode H-Index II

    原题链接在这里:https://leetcode.com/problems/h-index-ii/ 题目: Follow up for H-Index: What if the citations a ...

随机推荐

  1. mybatis源码追踪2——将结果集映射为map

    org.apache.ibatis.binding.MapperMethod中execute方法 ...} else if (method.returnsMap()) { result = execu ...

  2. dataTable 参数说明

    下面是一些常用的参数列表,比较常用或者有价值的标示为绿色. 功能参数(Features) 参数名 说明 参考值 默认值 autoWidth 定义是否由控件自动控制列宽 Boolean true def ...

  3. 解决ORA-21561: OID generation failed

    解决ORA-21561 在linux上使用sqlplus连接oracle数据库 [root@china ~]# sqlplus test/test@ORCL SQL Production :: Cop ...

  4. Collection、Set、List概念上的区别及关联

    类图如下:

  5. Vundle,Vim 的 Bundle(转)

    长久以来,我管理 Vim 配置的方式都非常原始—— zip 打包,然后发到邮箱上.偶尔会发生忘记备份,或者配置混淆的状况,不过由于懒筋发作,竟然这个方案就这么用了两年. 终有一天,我觉得这个方法太笨了 ...

  6. Java之集合(七)Map

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7327216.html 1.前言 按照顺序,本章本是要对Set的相关类进行介绍及讲解的.但是对于其实现有所了解的都 ...

  7. 腾讯云域名申请+ssl证书申请+springboot配置https

    阿里云域名申请 域名申请比较简单,使用微信注册阿里云账号并登陆,点击产品,选择域名注册 输入你想注册的域名 进入域名购买页面,搜索可用的后缀及价格,越热门的后缀(.com,.cn)越贵一般,并且很可能 ...

  8. Sublime Text 3快捷键的使用技巧(python)

    Sublime Text 3非常实用,但是想要用好,一些快捷键不可或缺,所以转了这个快捷键汇总. 先拉出来说几个切身体会最常用, Ctrl+Z 撤销. Ctrl+Y 恢复撤销 Alt+Shift+2  ...

  9. linux把程序添加到全局环境变量

    比如把, nginx服务放到全局变量 ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ /usr/local/bin/就是环境变量目录

  10. docker容器网络通信原理分析(转)

    概述 自从docker容器出现以来,容器的网络通信就一直是大家关注的焦点,也是生产环境的迫切需求.而容器的网络通信又可以分为两大方面:单主机容器上的相互通信和跨主机的容器相互通信.而本文将分别针对这两 ...