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. jzoj2941

    我們可以暴力枚舉每一個人分幾個糖果,再暴力統計答案即可 每次遞歸下去可以從1-n號人,決定選多少個糖果再遞歸 #include<bits/stdc++.h> using namespace ...

  2. jzoj3027

    根據打表找規律可得ans=c(k−n,n)∗an∗bk−nans=c(k-n,n)*a^n*b^{k-n}ans=c(k−n,n)∗an∗bk−n #include<bits/stdc++.h& ...

  3. jzoj5925

    tj:這道題題解有錯 水法ac代碼如下: #include<bits/stdc++.h> using namespace std; typedef long long ll; ll t,n ...

  4. spring JdbcTemplate批量插入以及单个插入时获取id

    1. 批量更新插入 jdbcTemplate.batchUpdate(String sql, List<Object[]> batchArgs) Object[]数组的长度为每条记录的参数 ...

  5. flask-mysqldb安装时EnvironmentError: mysql_config not found

    安装时候的日志如下: sh: : mysql_config: not found Traceback (most recent call last): File , in <module> ...

  6. javascript 易错知识点合集

    为什么 typeof null === 'object' 原理是这样的,不同的对象在底层都表示为二进制,在JavaScript中二进制前三位都为0的话会被判断为 object 类型, null 的二进 ...

  7. nginx请求频率限制模块ngx_http_limit_req_module

    模块: ngx_http_limit_req_module 作用: 限制客户端请求频率,防止恶意攻击 配置示例: http { limit_req_zone $binary_remote_addr z ...

  8. nginx-限制后端服务器连接数

    http加 limit_conn_zone $binary_remote_addr zone=perip:10m;limit_conn_zone $server_name zone=perserver ...

  9. MATLAB基础操作笔记

    A( i , : ) 表示 A矩阵的第 i 行所有元素 A( : , j ) 表示 A矩阵的第 j 列所有元素 A( : , : ) 表示 A矩阵的 所有元素 定义函数时,函数中有嵌套函数需要在结束时 ...

  10. R程序包

    === 数据基础操作 ===reshape2 横向.纵向做数据变换,例如把纵向堆叠在数据库中的证券行情数据转换成一个按照不同证券代码横向排列,按照时间纵向排列收盘价的数据表stringr 方便地用正则 ...