【Lintcode】135.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.
Example
Given candidate set [2,3,6,7]
and target 7
, a solution set is:
[7]
[2, 2, 3]
题解:
这个题和LeetCode不一样,这个允许重复数字产生,那么输入[2,2,3],7 结果为[2, 2, 3], [2, 2, 3], [2, 2, 3];要么对最后结果去除重复数组,要么在处理之前就对candidates数组去重复,或者利用set不重复的特点添加数组。
Solution 1 ()
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
if (candidates.empty()) {
return {{}};
}
set<vector<int> > res;
vector<int> cur;
sort(candidates.begin(), candidates.end());
dfs(res, cur, candidates, target, ); return vector<vector<int> > (res.begin(), res.end());
}
void dfs(set<vector<int> > &res, vector<int> &cur, vector<int> candidates, int target, int pos) {
if (!cur.empty() && target == ) {
res.insert(cur);
return;
}
for (int i = pos; i < candidates.size(); ++i) {
if (target - candidates[i] >= ) {
cur.push_back(candidates[i]);
dfs(res, cur, candidates, target - candidates[i], i);
cur.pop_back();
} else {
break;
}
}
}
};
【Lintcode】135.Combination Sum的更多相关文章
- 【Lintcode】153.Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 【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 ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】039. Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】216. Combination Sum III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...
随机推荐
- ReentrentLock重入锁
ReentrentLock lock=new ReentrentLock(); lock.lock(); //锁的代码 finally{ lock.unlock(); } ReentrentLock ...
- 编写mipsel mt7620 Led驱动(一)
1.看原理图中知芯片上66引脚控制一个LED 2.在Datasheet中找出GPIO pin 3.在ProgrammingGuid System Contrl中找到GPIO控制寄存器地址: 4.控制 ...
- 【Unity3D】【NGUI】Atlas的动态创建
NGUI版本号:3.6.5 1.參见SZUIAtlasMakerRuntimeTest设置对应的值以上值须要提前设置好 2.没有检查是否atlas可以正确创建,自己可以改,增加返回值 3.代码都是在N ...
- 深入Asyncio(七)异步上下文管理器
Async Context Managers: async with 在某些场景下(如管理网络资源的连接建立.断开),用支持异步的上下文管理器是很方便的. 那么如何理解async with关键字? 先 ...
- ubuntu apt 主要命令及参数
1. apt-cache search package 搜索安装包 2. apt-cache search all 搜索所有安装包 3. apt-cache show package 显示安装包信息 ...
- oracle角色(role)概念
一个角色是一组特权,它可以授权给用户或其它角色. 特权有:create table,select on boss ,create session,insert on boss,update on bo ...
- Angular1.0路由的Hashbang和HTML5模式
原文答主jupiter http://stackoverflow.com/questions/16677528/location-switching-between-html5-and-hashban ...
- 资源:Localization – 本地化
Resource Dictionary –资源字典 所有的资源项在最终都会被整合到Resource Dictionary中的,也就是说无论是FrameworkElement的Resources,还是W ...
- 使用SqlDependency监听MSSQL数据库表变化通知
SqlDependency提供了这样一种机制,当被监测的数据库中的数据发生变化时,SqlDependency会自动触发OnChange事件来通知应用程序,从而达到让系统自动更新数据(或缓存)的目的. ...
- iOS App打包上架超详细流程
https://www.jianshu.com/p/817686897ec1?open_source=weibo_search