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的更多相关文章

  1. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  2. js中sum(2,3,4)和sum(2)(3)(4)都返回9并要求扩展性

    网上有很多关于sum(1)(2)(3),sum(1,2,3)之类的面试题要求输出相同的结果6并要求可以满足扩展,即有多个参数时也能符合题设的要求,所以自己写了部分例子可以大概满足这些面试题的要求 &l ...

  3. 编写一个求和函数sum,使输入sum(2)(3)或输入sum(2,3),输出结果都为5

    昨天的笔试题,做的一塌糊涂,题目考的都很基础而且很细,手写代码对我来说是硬伤啊.其中有一道是这个,然而看到题目的时候,根本没有想到arguments:然后现在就恶补一下. arguments:用在函数 ...

  4. [Swift]LeetCode40. 组合总和 II | Combination Sum II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [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 ...

  6. [LeetCode 112 113] - 路径和I & II (Path Sum I & II)

    问题 给出一棵二叉树及一个和值,检查该树是否存在一条根到叶子的路径,该路径经过的所有节点值的和等于给出的和值. 例如, 给出以下二叉树及和值22: 5         / \       4  8  ...

  7. 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 ...

  8. [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 ...

  9. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

随机推荐

  1. 深度克隆(对象、数组)--------百度IFE前端task2

    var srcObj = { a: 1, b: { b1: ["hello", "hi"], b2: "JavaScript" }}; co ...

  2. ie6下:png图片不透明 和 背景图片为png的节点的内部标签单击事件不响应

    1.png图片不透明 少量图片时:使用滤镜: _background:none; _filter:prodig:DXImageTransform.Microsoft.AlphaImageLoader( ...

  3. 单服务员排队模拟100天matlab实现

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang %单服务员排队模型模拟100天 clear clc day = 100 ;s = zeros(1, ...

  4. IOS 如何选择delegate、notification、KVO?

    IOS 如何选择delegate.notification.KVO? 博客分类: IOS   前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有 ...

  5. NSNotificationCenter基础知识

    NSNotificationCenter基础知识   Notification的工作机制 1.对应用程序中其他地方发生的事件(比如增加一条数据库记录)感兴趣的对象,会向通告中心(Notificatio ...

  6. STL string常用操作指令

    s.insert(pos,args); 在pos之前插入args指定的字符.pos可以是一个下标或一个迭代器.接受下标的版本返回一个指向s的引用;接受迭代器的版本返回指向第一个插入字符的迭代器. s. ...

  7. 磁珠(FB)的选用

    1. 磁珠(FB)的单位是欧姆,而不是亨特,这一点要特别注意.因为磁珠的单位是按照它在某一频率 产生的阻抗来标称的,阻抗的单位也是欧姆.磁珠的 DATASHEET上一般会提供频率和阻抗的特性曲线图,一 ...

  8. Qt创建和使用动态链接库

    一.创建共享库 1.新其他建项目,选择C++库 2.选择共享库,并取项目名称,单击下一步.这里取名位mylib 3.按默认配置单击下一步至模块选项,选择所需支持的模块.这里勾选Qtcore和QtGui ...

  9. java解析网页的内容

    有时候,我们需要在java程序中获取一个连接,然后解析连接后,获取连接返回的内容结果来解析.准确的说是解析一个链接. 以下代码时解析百度首页的链接,获取的html代码的效果: public stati ...

  10. coalesce和nvl函数

    coalesce 函数 : Oracle COALESCE函数语法为COALESCE(表达式1,表达式2,...,表达式n),n>=2,此表达式的功能为返回第一个不为空的表达式,如果 都为空则返 ...