Leetcode39.Combination Sum组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明:
所有数字(包括 target)都是正整数。
解集不能包含重复的组合。
示例 1:
输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ]
示例 2:
输入: candidates = [2,3,5], target = 8, 所求解集为: [ [2,2,2,2], [2,3,3], [3,5] ]
先排序,后剪枝,避免重复。
这类问题解决重复的操作一般是先进行排序。
bool cmp1(int x, int y)
{
return x < y;
}
class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > combinationSum(vector<int>& candidates, int target)
{
int len = candidates.size(www.dasheng178.com);
sort(candidates.begin(), candidates.end(), cmp1);
vector<int> temp;
DFS(candidates, target, temp, 0);
return res;
}
void DFS(vector<int> candidates, int val, vector<int> &v, int cnt)
{
if(val == 0)
{
res.push_back(www.gcyl159.com/);
return;
}
for(int i = cnt; i <www.michenggw.com/ candidates.size(); i++)
{
if(candidates[i] <www.yigouyule2.cn = val)
{
v.push_back(candidates[i]);
DFS(candidates, val - candidates[i], v, i);
v.pop_back();
}
}
}
Leetcode39.Combination Sum组合总和的更多相关文章
- 【LeetCode】Combination Sum(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- 039 Combination Sum 组合总和
给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T.可以从 C 无限次数中选择相同的数字.说明: 所有数字(包括目标)都是正整数. 解集合 ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Leetcode40. Combination Sum组合总数2 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- LeetCode39 Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- HDU 1223 打表 + 大数
http://acm.hdu.edu.cn/showproblem.php?pid=1223 一般遇到这些题,我都是暴力输出前几项,找规律.未果. 然后输出n = 1时候,以A开始,有多少个答案, n ...
- Oracle、MySQL和SqlServe分页查询的语句区别
★先来定义分页语句将要用到的几个参数: int currentPage ; //当前页 int pageRecord ; //每页显示记录数 以之前的ADDRESSBOOK数据表为例(每页显示10条记 ...
- 全文索引Elasticsearch,Solr,Lucene
最近项目组安排了一个任务,项目中用到了全文搜索,基于全文搜索 Solr,但是该 Solr 搜索云项目不稳定,经常查询不出来数据,需要手动全量同步,而且是其他团队在维护,依赖性太强,导致 Solr 服务 ...
- codevs 5438 zbd之难题(水题日常)
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...
- Python游戏-实现键盘控制功能
要想实现键盘控制作用,就需要给游戏键盘监听事件利用pygame模块的key.get_pressed()方法,来检测按键是否按下 key_press =pygame.key.get_pressed() ...
- exportfs - 管理NFS共享文件系统列表
概述 (SYNOPSIS) /usr/sbin/exportfs [-avi] [-o options,..] [client:/path ..] /usr/sbin/exportfs -r [-v] ...
- js中重载问题
在js中是没有重载的 但是 Arguments对象(可以实现模拟重载的效果) 利用arguments对象的length属性,可以获取函数接收的参数的个数 例如: function add(){ i ...
- Vector(同步)和ArrayList(异步)异同
//1 同步 异步 //未响应 = 假死 占用内存过多 内存无法进行处理 //请求的方式 :同步 异步 //网页的展现过程中(同步):1. css文件的下载 2.js文件的下载 3.下载你当前的htm ...
- attr和prop的区别
由于prop(property的缩写)和attr(attribute的缩写)翻译成汉语,均有“特性.属性”等意思的原因,导致大家容易混淆分不清. (1)在处理自定义时属性时,用attr(),若用pro ...
- SQL 语句解决实际问题
在项目开发过程中,遇到数据库的查询问题 一.查询某表字段的信息 select * from syscolumns SELECT object_id('TB_KYChildProject') selec ...