Combination Sum,Combination Sum II,Combination Sum III
39. 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的所有不重复组合,数据源中的数据可以重复使用
深度优先+回溯,可剪枝
class Solution {
private:
void dsf(vector<int>& datas,int start,vector<vector<int>>& res,vector<int>& oneRes,int target,int curSum)
{
for(int i=start;i<datas.size();++i){ if(i>start && datas[i]==datas[i-]){
continue;
} if(curSum + datas[i] > target){//break跳出循环,剪枝
break;
} if(curSum + datas[i] == target){//break跳出循环,剪枝
oneRes.push_back(datas[i]);
res.push_back(oneRes);
oneRes.pop_back();
break;
} oneRes.push_back(datas[i]);
curSum += datas[i]; dsf(datas,i,target,res,oneRes,curSum); curSum -= datas[i];
oneRes.pop_back();
}
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector<vector<int>> res;
vector<int> oneRes;
dsf(candidates,,target,res,oneRes,);
return res;
}
};
40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
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 10,1,2,7,6,1,5
and target 8
,
A solution set is: [1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
这题跟上面那题没有什么区别
class Solution {
private:
void dsf(vector<int>& datas,int start,vector<vector<int>>&res,vector<int>& oneRes,int target,int curSum){
for(int i=start;i<datas.size();++i){ if(i>start && datas[i]==datas[i-]){
continue;
} int tmpSum = curSum + datas[i]; if(tmpSum > target){
break;
} if(tmpSum == target){
oneRes.push_back(datas[i]);
res.push_back(oneRes);
oneRes.pop_back();
break;
} oneRes.push_back(datas[i]); dsf(datas,i+,target,res,oneRes,tmpSum); oneRes.pop_back(); }
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector<vector<int>> res;
vector<int> oneRes;
dsf(candidates,,target,res,oneRes,);
return res;
}
};
216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Ensure that numbers within the set are sorted in ascending order.
Example 1:
Input: k = 3, n = 7
Output:
[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
这题可以使用与上面两题一样的方法
class Solution {
private:
void dfs(int start,vector<vector<int>>&res,vector<int>& oneRes,int k,int target,int curSum)
{
for(int i=start;i<=;++i){ if(curSum + i > target){
break;
} if(curSum + i == target && k-==){
oneRes.push_back(i);
res.push_back(oneRes);
oneRes.pop_back();
break;
} if(k==){
break;
} oneRes.push_back(i);
curSum += i; dfs(i+,res,oneRes,k-,target,curSum); curSum -= i;
oneRes.pop_back();
}
}
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> res;
vector<int> oneRes;
dfs(,res,oneRes,k,n,);
return res;
}
};
当然,这题还可以使用ksum的方法,先算法2sum,然后3sum...ksum
Combination Sum,Combination Sum II,Combination Sum III的更多相关文章
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- js中sum(2,3,4)和sum(2)(3)(4)都返回9并要求扩展性
网上有很多关于sum(1)(2)(3),sum(1,2,3)之类的面试题要求输出相同的结果6并要求可以满足扩展,即有多个参数时也能符合题设的要求,所以自己写了部分例子可以大概满足这些面试题的要求 &l ...
- 编写一个求和函数sum,使输入sum(2)(3)或输入sum(2,3),输出结果都为5
昨天的笔试题,做的一塌糊涂,题目考的都很基础而且很细,手写代码对我来说是硬伤啊.其中有一道是这个,然而看到题目的时候,根本没有想到arguments:然后现在就恶补一下. arguments:用在函数 ...
- [Swift]LeetCode40. 组合总和 II | Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [Swift]LeetCode113. 路径总和 II | Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode 112 113] - 路径和I & II (Path Sum I & II)
问题 给出一棵二叉树及一个和值,检查该树是否存在一条根到叶子的路径,该路径经过的所有节点值的和等于给出的和值. 例如, 给出以下二叉树及和值22: 5 / \ 4 8 ...
- Leetcode题 112 和 113. Path Sum I and II
112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
随机推荐
- 对于js原型和原型链继承的简单理解(第二种,对象冒充)
关键代码 this.parent = Cat; this.parent.apply(this); function Cat(){ this.climb = function(){ aler ...
- 表单控件之select
一.表单控件之表单 1.依次获取表单里的所有控件: for (i = 0; i < document.getElementById("formName").length; i ...
- ecstore生成二维码
利用phpqrcode库生成二维码: /* *二维码添加 */ ////////////////////////////////////// /*引入文件*/ @include(APP_DIR.'/i ...
- apscheduler的使用
最近一个程序要用到后台定时任务,看了看python后台任务,一般2个选择,一个是apscheduler,一个celery.apscheduler比较直观简单一点,就选说说这个库吧.网上一搜索,晕死,好 ...
- laravel实现第三方登录(qq登录)
首先composer安装依赖: composer require socialiteproviders/qq 注册服务提供者(同时注释掉原有的Socialite提供者): 'providers' =& ...
- 自定义Window进入和退出效果(转)
看了android的源代码和资源文件,终于明白如何去修改设置Dialog和Activity的进入和退出效果了. 设置Dialog首先通过getWindow()方法获取它的窗口, 然后通过getAttr ...
- 基于Redis的消息订阅/发布
在工业生产设计中,我们往往需要实现一个基于消息订阅的模式,用来对非定时的的消息进行监听订阅. 这种设计模式在 总线设计模式中得到体现.微软以前的WCF中实现了服务总线 ServiceBus的设计模式. ...
- 关于TCP的三次握手和四次分手(整理)
这个协议非常重要,这里把它的链接和释放整理一下 首先是三次握手: 1. 客户端发起,像服务器发送的报文SYN=1,ACK=0,然后选择了一个初始序号:seq=x. SYN是干什么用的? 在链接的时候 ...
- Linux企业级项目实践之网络爬虫(10)——处理HTTP状态码
HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.所有状态码的第一个数字代表了响应的五种状态之一.他们分别是:消息(1字头)成功(2字头)这一类型的 ...
- c语言筛选质数
#include <stdio.h> #include <stdlib.h> #include <math.h> int isit(int num) { int i ...