题目:

Given a set of candidate numbers (C) (without duplicates) 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.
  • 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]
]

  

题解:

Solution 1 ()

class Solution {
public:
void dfs(vector<vector<int>>& vv, vector<int>& v, vector<int> candidates, int target, int sum) {
if(sum == target) {
vector<int>* tmp = new vector<int>;
*tmp = v;
sort((*tmp).begin(), (*tmp).end());
if(find(vv.begin(), vv.end(), *tmp) == vv.end())
vv.push_back(*tmp);
delete tmp;
return;
}
if(sum > target) return;
for(int i=; i<candidates.size(); ++i) {
v.push_back(candidates[i]);
dfs(vv, v, candidates, target, sum+candidates[i]);
v.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> vv;
vector<int> v;
dfs(vv, v, candidates, target, );
return vv;
}
};

【LeetCode】039. Combination Sum的更多相关文章

  1. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  2. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  3. 【LeetCode】40. Combination Sum II (2 solutions)

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

  4. 【LeetCode】39. Combination Sum (2 solutions)

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

  5. 【LeetCode】040. Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  6. 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】216. Combination Sum III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...

  8. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  9. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

随机推荐

  1. C 语言学习 3

    [程序3] 题目:一个整数,它加上100后是一个全然平方数.再加上168又是一个全然平方数.请问该数是多少? 1.程序分析:在10万以内推断.先将该数加上100后再开方,再将该数加上268后再开方,假 ...

  2. 使用Chrome(PC)调试移动设备上的网页

    最早开始调试移动端网页时,本人都是采取PC上改几行代码,手机上刷新一下看效果这种笨方法来开发的,效率低而且容易让人抓狂.最近偶然发现原来可以使用PC上的浏览器来调试移动设备,不由得感叹相逢恨晚. 工具 ...

  3. Linux内核编译过程分析

    http://pan.baidu.com/s/1mgtACVu 其中是我总结生成的一些文档,以便于理解当我们输入make uImage后,系统是怎么一步一步生成uImage的,我采用的是逆向分析的方法 ...

  4. 创业做移动互联网App的4个注意事项

    导语:大多数人对于做App还是比較盲目,有个想法立刻就去做了.做出来了才忽然想到市场和推广.我把做移动 互联网App注意事项情给大家列下. 文| 移动互联网李建华 近 来,常常有人问我关于推广的事情, ...

  5. 【BZOJ4240】有趣的家庭菜园 树状数组+贪心

    [BZOJ4240]有趣的家庭菜园 Description 对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物.JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N.IO ...

  6. Redis实现主从复制(转)

    一.Redis的Replication: 这里首先需要说明的是,在Redis中配置Master-Slave模式真是太简单了.相信在阅读完这篇Blog之后你也可以轻松做到.这里我们还是先列出一些理论性的 ...

  7. js闭包实际用途

    闭包例:防止双击 在线商店的购物车里,为防止“多重购买”,需要防止按钮被双击. 下面用“jQuery + 闭包”来实现这一功能. HTML <form name="frm" ...

  8. C#根据Type获取默认值

    简单的获取某变量类型的默认值 在c#中为我们提供了default(),但是default的参数是具体的类名, 如何根据变量类型的Type获取默认值Code如下: 1 public static obj ...

  9. Generalised Policy Iteration With Monte-Carlo Evaluation

    http://www0.cs.ucl.ac.uk/staff/d.silver/web/Teaching_files/control.pdf

  10. 360手机助手: App上架(提交资料)注意事项

    提交app资料尤其注意: .keystore和密码?由HBuilder提供公有证书: 签名证书KeyStore证书别名:hbuilder证书密码:123456keystore密码:123456 360 ...