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. 01.Editplus+Lua配置

    学习一门语言有一款简单顺手的编辑工具很重要,我使用Editplus要多一点:就想能不能加上Lua支持,网上一搜还还不少.现把配置记录下来,也算做个笔记吧! Editplus版本是中文3.41(网上下吧 ...

  2. C# 过滤SerialPort端口

    C#中的SerialPort类只能通过SerialPort.GetPortNames()来获取端口名字,而且出来都是COM+数字的字样,如果我要识别COM端口是什么设备的,它就无能为力了,此时我们可以 ...

  3. 兼容IE的渐变

    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=, startColorstr=#1471da, endColorstr ...

  4. .cshrc

    使用set和setenv命令可以设置shell选项或者列出shell变量 在C Shell 里, set 定义局部变量, setenv定义全局变量   1.set   set 变量名 = 内容: s2 ...

  5. [T]各种字符串Hash函数比较

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  6. commons-beanutils使用

    Jakarta Commons项目提供了相当丰富的API,我们之前了解到的Commons Lang只是众多API的比较核心的一小部分而已.Commons下面还有相当数量的子项目,用于解决各种各样不同方 ...

  7. 剑指offer中二进制中1的个数

    容易想到的是将n一位一位的和1进行比较,产生如下代码 但是这样的话会出下面的问题 那么就是原数据不动,将1依次移动进行比较有如下的代码 一种更简单的方法是: 这样做的思路是 总而言之

  8. 主题模型-LDA浅析

    (一)LDA作用 传统判断两个文档相似性的方法是通过查看两个文档共同出现的单词的多少,如TF-IDF等,这种方法没有考虑到文字背后的语义关联,可能在两个文档共同出现的单词很少甚至没有,但两个文档是相似 ...

  9. JDBC开发模式

    一]代码模块———Demo.java public class Demo { private static Connection connection; private static Statemen ...

  10. hdu 找新朋友

    #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ]; ...