【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 ...
随机推荐
- 使用css counter来美化代码片段的样式
博客园默认的代码片段样式不太美观,特别是复制代码时会把前面的行号也复制下来,操作起来比较麻烦.最近看到一种使用CSS计数器来美化代码片段的方法,于是研究了一下计数器的使用,在此做个笔记. 这是官网的例 ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- sigar 监控服务器硬件信息
转载 http://www.cnblogs.com/jifeng/archive/2012/05/16/2503519.html 通过使用第三方开源jar包sigar.jar我们可以获得本地的信息 1 ...
- python之Matplotlib 和Numpy
1.matplotlib http://www.cnblogs.com/TensorSense/p/6802280.html https://wenku.baidu.com/view/e1c15c9d ...
- VS中Component Class、User Control及Custom Control的区别 .
.NET Framework 为您提供了开发和实现新控件的能力.除了常见的用户控件外,现在您会发现,您可以编写能执行自身绘图的自定义控件,甚至还可以通过继承扩展现有控件的功能.确定创建何种类型的控件可 ...
- 兔子的晚会 2016Vijos省选集训 day1
兔子的晚会 (xor.c/pas/cpp)============================= 很久很久之前,兔子王国里居住着一群兔子.每到新年,兔子国王和他的守卫总是去现场参加晚会来欢庆新年. ...
- js为Object对象动态添加属性和值 eval c.k c[k]
const appendInfo = () => { const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/maste ...
- 【网络与系统安全】利用burpsuite进行重放攻击
重放攻击的定义 所谓重放攻击就是攻击者发送一个目的主机已接收过的包,来达到欺骗系统的目的,主要用于身份认证过程. 原理 重放攻击的基本原理就是把以前窃听到的数据原封不动地重新发送给接收方.如果攻击者知 ...
- 【题解】Counting D-sets(容斥+欧拉定理)
[题解]Counting D-sets(容斥+欧拉定理) 没时间写先咕咕咕. vjCodeChef - CNTDSETS 就是容斥,只是难了一二三四五\(\dots \inf\)点 题目大意: 给定你 ...
- Linux就该这么学--命令集合10(vim编辑器)
1.vim编辑器的命令模式中常用的快捷键: dd 删除(剪切)光标所在整行 5dd 删除(剪切)从光标处开始的5行 yy 复制光标所在整行 5yy 复制从光标处开始的5行 p 将之前删除(dd)或复制 ...