【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 ...
随机推荐
- Wordpress中文章的特色图像Featured Image究竟存在哪里?
最近项目需要,分析了一下Wordpress的特色图像 Feature Image的上传.保存方式,这一分析觉得Wordpress的数据结构设计还真是有想法. 先简单说一下结论: Wordpress中图 ...
- MySQL 用户管理及权限管理
MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...
- Java:字符串类简单的正则表达式
class Test { public static void main(String[] args) { String str = "xia..as....yuan.com"; ...
- 开启otl的64位长整数支持
要开启OTL的64位长整数支持,必须先定义宏 #define OTL_BIGINT __int64 // VC++, Borland C++ 或者 #define OTL_BIGINT long lo ...
- iOS开发-数据选择UIPickerView
UIPickerView开发一般选择区域或者分级数据的时候会使用到,类似于前端中用到树状结构,不过PC上一般都是从上到下的分级,使用UIPickView是从左到右实现,可以动态的设置UIPickVie ...
- Java基础(十一):接口
一.接口: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是 ...
- IOS之NSFileManager 和NSFileHandle
在现阶手机app的临时缓存文件渐渐增多,在app开发中对于移动设备文件的操作越来越多,我们IOS中对于文件的操作主要涉及两个类NSFileManager 和NSFileHandle,下面我们就看看如何 ...
- DIRECT Project
http://www.healthit.gov/policy-researchers-implementers/direct-project Launched in March 2010 as a p ...
- mybatis如何根据mapper接口生成其实现类(springboot)
序 mybatis里头给sqlSession指定执行哪条sql的时候,有两种方式,一种是写mapper的xml的namespace+statementId,如下: public Student fin ...
- 今天微信小程序发现wx.request不好使了,调试报错: 小程序要求的 TLS 版本必须大于等于 1.2
今天微信小程序发现wx.request不好使了,调试报错: 小程序要求的 TLS 版本必须大于等于 1.2 查官方文档 解决方法 在 PowerShell中运行以下内容, 然后重启服务器 # Enab ...