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. MySQL(动态执行SQL)

    day61 防sql注入 delimiter \\ CREATE PROCEDURE p4 ( ), in arg int ) BEGIN set @xo = arg; PREPARE xxx FRO ...

  2. java 中什么是aop

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  3. javascript浅拷贝深拷贝理解记录

    javascript的深拷贝和浅拷贝问题几乎是面试必问的问题.好记性不如烂笔头,特此来记录一下自己对深拷贝浅拷贝的理解. 顾名思义,拷贝就是copy复制,在js中可以浅而理解为对一个对象或者数组的复制 ...

  4. 使用dev-tool定位页面性能瓶颈

    这是部门同事的一次内部分享,听完后受益颇多,趁着记忆还算新鲜,赶紧记录一波. 从 dev-tool 看页面 parse 过程 时间都去哪儿了 当浏览器发送一个请求到接受所有响应数据截止,这个过程发生了 ...

  5. gulp-load-task 解决 gulpfile.js 过大的问题

    当我们在项目中使用gulp来实现前端自动化时,常常因任务太多导致gulpfile.js越来越臃肿,增加后期维护/变更成本.在计算机科学领域中,分治可以将我们的项目变得井然有序.所以,我们利用这个理念, ...

  6. 【xsy1154】 DNA配对 FFT

    题目大意:给你一个字符串$s$和字符串$w$,字符集为${A,T,C,G}$,你要在字符串$s$中选出一个与$w$长度相同的子串,使得这两个串的差异度最小. 两个字符$c1$,$c2$的差异度为给定的 ...

  7. Mac下使用zsh不执行/etc/profile文件

    Mac下使用了zsh会不执行/etc/profile文件,当然,如果用原始的是会执行. 转而执行的是这两个文件,每次登陆都会执行: ~/.zshrc与/etc/zshenv与/etc/zshrc 所以 ...

  8. 搭建互联网架构学习--004--centos安装Mysql

    Mysql安装 1. yum安装mysql yum -y install mysql-server 2. 启动mysql服务 启动mysql:service mysqld start 查看mysql的 ...

  9. FoxitReader软件下载并安装(图文详解)

    不多说,直接上干货! FoxitReader官方网址:https://www.foxitsoftware.com/downloads/ 结束 欢迎大家,加入我的微信公众号:大数据躺过的坑        ...

  10. Java的try-catch-finally

    Javac语法糖之TryCatchFinally 如下引用文章:https://help.semmle.com/wiki/display/JAVA/Finally+block+may+not+comp ...