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. (原)ubuntu下使用ftp软件

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6121663.html 参考网址: http://tieba.baidu.com/p/387426074 ...

  2. uva 11536 - Smallest Sub-Array

    题目大意:按照题目中的要求构造出一个序列,找出最短的子序列,包含1~k. 解题思路:先根据题目的方法构造出序列,然后用Towpointer的方法,用v[i]来记录当前[l, r]中有几个i:当r移动时 ...

  3. AngularJS自定义表单验证

    <!doctype html> <html ng-app="myApp"> <head> <script src="G:\\So ...

  4. swift闭包-备

    我给Swift 中的闭包一个定义:闭包是自包含的匿名函数代码块,可以作为表达式.函数参数和函数返回值,闭包表达式的运算结果是一种函数类型. Swift中的闭包类似于Objective-C中的代码块.J ...

  5. Ubuntu 下安装使用文件比较合并图形工具Meld

    Meld是一款跨平台的文件比较合并工具使用Python开发,具体内容参照官网:http://meldmerge.org/ 注意以下环境要求: Requirements Python 2.7 (Pyth ...

  6. ubuntu 终端只显示当前目录名称

    修改.bashrc文件: 原来: #修改终端提示颜色 color_prompt=yes if [ "$color_prompt" = yes ]; then PS1='${debi ...

  7. document.body.scrollTop vs document.documentElement.scrollTop

    window.addEventListener("scroll", function () { if (document.body.scrollTop >= window.i ...

  8. hdu 1385 Minimum Transport Cost

    http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...

  9. poj1740 A New Stone Game

    题意:对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆. 真是好♂题,代码不长就是好♂题. 首先考虑两堆相同的 ...

  10. 黑马程序员_Java_多线程

    8.多线程 8.1.多线程概述 进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控制单元. 线程(例:FlashGet):就是进程中一个独立的控制单元.线 ...