leetcode111:combination-sum
题目描述
- 题目中所有的数字(包括目标数T)都是正整数
- 你给出的组合中的数字 (a 1, a 2, … , a k) 要按非递增排序 (ie, a 1 ≤ a 2 ≤ … ≤ a k).
- 结解集中不能包含重复的组合
[2, 2, 3]
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 (a 1, a 2, … , a k) must be in non-descending order. (ie, a 1 ≤ a 2 ≤ … ≤ a k).
- The solution set must not contain duplicate combinations.
For example, given candidate set2,3,6,7and target7,
A solution set is:
[7]
[2, 2, 3]
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector <vector<int>>res;
vector<int> temp;
sort(candidates.begin(),candidates.end());
Sum(res,temp,candidates,0,target);
return res;
}
void Sum(vector <vector <int>> &res,vector<int> &temp,vector<int >&candidates,int k,int target){
if (target==0){
res.push_back(temp);
return ;
}
if (target <0 || k>=candidates.size())
return ;
vector<int> t =temp;
temp.push_back(candidates[k]);
Sum(res,temp,candidates,k,target-candidates[k]);
Sum(res,t,candidates,k+1,target);
}
};
leetcode111:combination-sum的更多相关文章
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
随机推荐
- P3419 [POI2005]SAM-Toy Cars / SP688 SAM - Toy Cars
一道很妙的贪心题 题面 我们考虑当我们插入时会面临的两种情况 当地上的玩具,不满 \(k\) 个时,那我们直接放就可以了. 当满了 \(k\) 个的时候,我们就要从地上拿出一个来给当前的腾位置. 这就 ...
- sql注入里关键字绕过的发现
网上大量文章,甚至<黑客攻防技术实战宝典-WEB实战篇>里面都说一些关键字如 select 等绕过可以用注释符/**/. 例如: select,union.可以用 ,se/**/lect, ...
- Linux配置Docker
Centos6.8 1.查看自己的内核 [1].uname [root@host79 ~]# uname -r 2.6.32-642.el6.x86_64 [2].查看CentOS版本信息 CentO ...
- turtle库元素语法分析
一.turtle原理理解: turtle库是Python中一个有趣的图形绘制函数库.原名(海龟),我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形. 对于小海龟 ...
- 持续集成工具之jenkins+sonarqube做代码扫描
上一篇我们主要聊了下代码质量管理平台sonarqube的安装部署以及它的工作方式做了简单的描述和代码扫描演示:回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13 ...
- Ambari仓库安装教程
Ambari仓库安装教程 如果用户需要后续使用Ambari server进行安装Hadoop则必须搭建一个内部的yum源,否则直接下载速度将会很慢,当然该服务仅要搭建一个即可,可以一直使用. 一.Ce ...
- js实现无缝连接轮播图(二)实现自定义属性,根据banner图片的数量动态生成小圆点
<!-- 这个animate.js 必须写到 index.js的上面引入 --><script src="js/animate.js"></scrip ...
- C# 使用MySQL事务的使用方法
//使用事务来处理多条数据,如果不成功则回滚 public void getCheckListSubmit() { string _conStr = "................&qu ...
- 阿里技术专家十五问,真题面试刀刀见肉,快来和阿里面试官battle
引言 2020阿里巴巴专家组出题,等你来答: 题目:如何判断两个链表是否相交 出题人:阿里巴巴新零售技术质量部 参考答案: $O(n^2)$: 两层遍历,总能发现是否相交 $O(n)$: 一层遍历,遍 ...
- hdu6115 Factory (LCA + 倍增)
Factory Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total ...