【LeetCode】39. Combination Sum (2 solutions)
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.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- 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]
寻找target成员的过程中,如果candidates[i]是组成target的成员之一,那么寻找target-candidates[i]的子问题与原题就完全一致,因此是典型的递归。
参数列表中:result设为全局变量,用于记录所有可行的路径,因此使用引用(&);curPath是每次递归栈中独立部分,因此使用拷贝复制
解法一:使用map去重
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int> > ret;
map<vector<int>, bool> m;
vector<int> cur;
Helper(ret, cur, candidates, target, m, );
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> cur, vector<int> &candidates, int target, map<vector<int>, bool> &m, int ind)
{
if(target == )
{
if(m[cur] == false)
{
ret.push_back(cur);
m[cur] = true;
}
}
else
{
for(int i = ind; i < candidates.size() && candidates[i] <= target; i ++)
{// for each candidate
int val = candidates[i];
cur.push_back(val);
Helper(ret, cur, candidates, target-val, m, i); // duplication allowed
cur.pop_back();
}
}
}
};
解法二:
稍作分析可知,重复结果的原因在于candidates中的重复元素。
因为我们默认每个位置的元素可以重复多次,而不同位置的元素是不同的。
对candidates的排序及去重的目的就是防止结果的重复,比如7 --> 2,2,3/2,3,2/3,2,2
注:去重函数unique的用法
1、先排序,因为unique只会去掉连续元素中的重复元素
sort(candidates.begin(), candidates.end());
2、调用unique函数
vector<int>::iterator iter = unique(candidates.begin(), candidates.end());
执行完毕之后,返回的iter指向去重之后新数组的尾部,
例如:1,2,2,4,4,5
得到:1,2,4,5,?,?
^
iter
3、最后删除iter到end()之间的所有元素
candidates.erase(iter, candidates.end());
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<int>::iterator iter = unique(candidates.begin(), candidates.end());
candidates.erase(iter, candidates.end()); vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, candidates, target, );
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> cur, vector<int> &candidates, int target, int pos)
{
if(target == )
{
ret.push_back(cur);
}
else
{
for(int i = pos; i < candidates.size() && candidates[i] <= target; i ++)
{
//candidates[i] included
cur.push_back(candidates[i]);
//next position is still i, to deal with duplicate situations
Helper(ret, cur, candidates, target-candidates[i], i);
//candidates[i] excluded
cur.pop_back();
}
}
}
};
【LeetCode】39. Combination Sum (2 solutions)的更多相关文章
- 【LeetCode】39. Combination Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- 【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】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 【一天一道LeetCode】#39. Combination Sum
一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- 【LeetCode】216. Combination Sum III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】039. Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...
随机推荐
- [Android UI] ActionBar 自定义属性
actionbar 默认放在顶部, 如果在application或者activity中加入 android:uiOptions="splitActionBarWhenNarrow" ...
- [转]C++之运算符重载(1)
在前一节中曾提到过,C++中运行时的多态性主要是通过虚函数来实现的,而编译时的多态性是由函数重载和运算符重载来实现的.这一系列我将主要讲解C++中有关运算符重载方面的内容.在每一个系列讲解之前,都会有 ...
- 【BZOJ】【2434】【NOI2011】阿狸的打字机
AC自动机+DFS序+BIT 好题啊……orz PoPoQQQ 大爷 一道相似的题目:[BZOJ][3172][TJOI2013]单词 那道题也是在fail树上数有多少个点,只不过这题是在x的fail ...
- Mysql数据库事务及隔离级别学习测试
参考了这篇文章的一些内容: http://xm-king.iteye.com/blog/770721 记住以下这张表: 我在springdemo库里面建了一个表: CREATE TABLE `tx` ...
- 理解 CSS 的 z-index 属性
通常认为HTML页面是二维的,但实际上,CSS还有一个z-index属性,允许层叠元素. 所有的盒模型元素都处于三维坐标系中. 除了我们常用的横坐标和纵坐标, 盒模型元素还可以沿着“z轴”层叠摆放, ...
- Java基础(五):数组和Java方法
一.Java数组: 1.声明数组变量: 首先必须声明数组变量,才能在程序中使用数组.下面是声明数组变量的语法:注意: 建议使用 dataType[] arrayRefVar 的声明风格声明数组变量. ...
- effective C++中条款37:绝不又一次定义继承而来的缺省參数值
virtual 函数会动态绑定,而virtual函数的缺省參数值是静态绑定的. 用一个base类型的指针p去指向一个derived类对象.通过p调用虚函数时,会动态绑定到实际所指对象中的函数:用一个d ...
- java oracle thin 和 oci 连接方式实现多数据库的故障切换
java oracle thin 和 oci 连接方式实现多数据库的故障切换 一.thin方式 该种方式简便易用非经常见. 当中URL为 jdbc:oracle:thin:@(DESCRIPTION= ...
- 5. How to set up a Activity
1. Create a new xml in "layout" folder "splah.xml" <?xml version="1.0&qu ...
- 经典,HTML5游戏,超级玛丽
在线演示 在线演示 本地下载 这是一款使用HTML5开发的超级玛丽,有没有点儿时的记忆?长按向上键,可以跳的更高哦.如果你也喜欢可以当成休闲游戏,如果你是开发者,不防下载下来看看是如何生成的.