【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 ...
随机推荐
- FastJson和Gson和Json数据解析分析和用法
首先分析下目前号称最快的FastJson,这个是所有人都验证过的,解析速度确实比较快,不过也需要根据数据量来看,数据量小的时候,Gson性能要稍微优于FastJson,但在数据量大解析的情况下,Fas ...
- 500 OOPS: vsftpd: cannot locate user specified in 'chown_username':whoever
错误:500 OOPS: vsftpd: cannot locate user specified in 'chown_username':whoever解决方案:在vsftpd.conf中修改如下两 ...
- Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)
引用计数和字符串 内存中的常量字符串的空间分配与其他对象不同,他们没有引用计数机制 凡是自定义的对象都有引用计数机制: OC内部中对象分为可变对象(NSMutableString等)和不可变对象(NS ...
- ING【转载】处理大并发系列
一直在处理高可用高并发的服务.看到网上有一个系列的文章,写的不错.跟进一下. 一:http://blog.csdn.net/feitianxuxue/article/details/8936802 二 ...
- C#中属性PropertyInfo的使用
昨天编程遇到一个问题两个类字段都是二十多个,其中有十多个是相同的,需要将一个类的字段赋值给另外一个类,开始的自己想手动的一个个去赋值,后来想来一下C#基础知识,用PropertyInfo就可以解决类似 ...
- ButterKnife 注解 bindview
简介 官网:http://jakewharton.github.io/butterknife/ github:https://github.com/JakeWharton/butterknife 注意 ...
- 4. Add override methods to class
1. In the class, right click 2. "Scource" 3. "Override / Implement Menthods" 4. ...
- 在Foreda上安装apache-tomcat-7.0.42.tar.gz
开发环境JDK和Tomcat应该和部署环境一致,要不容易出现奇奇怪怪的问题.所以Aspire机器上的Tomcat要装一个新版本了. 装Tomcat基本等于一个解压和移动的过程,确实简单. 第一步:解压 ...
- sed 常用的命令
n: 读取一行,执行n,把当前行打印到标准输出,再读取一行,覆盖当前行,然后对模式空间执行一组模式/行为.N:读取一行,执行N,再读取一行,现在模式空间有两行内容,执行一组模式/行为.如下:[root ...
- swift学习之元组
元组在oc中是没有的.在swift中是新加的,学oc数组概念时还在想既然能够存储同样类型的元素,那不同类型的元素有没有东西存储呢,答案非常悲伤,oc没有元组这个概念.只是swift中加入了这个东西,也 ...