【leetcode】Combination Sum (middle)
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]
思路: 有规律的查找,避免重复。用递归得到所有的解
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int>> ans;
if(candidates.empty())
return ans;
sort(candidates.begin(), candidates.end()); //从小到大排序
recursion(ans, candidates, , target);
return ans;
}
void recursion( vector<vector<int> > &ans, vector<int> candidates, int k, int target)
{
static vector<int> partans;
if(target == ) //如果partans中数字的总和已经达到目标, 压入答案
{
ans.push_back(partans);
return;
}
if(target < )
return;
for(int i = k; i < candidates.size(); i++) //当前压入大于等于candidates[k]的数字
{
int sum = candidates[i];
while(sum <= target) //数字可以压入多次,只要和小于等于目标即可
{
partans.push_back(candidates[i]);
recursion(ans, candidates, i + , target - sum); //后面只压入大于当前数字的数,避免重复
sum += candidates[i];
}
while(!partans.empty() && partans.back() == candidates[i]) //状态还原
partans.pop_back();
}
}
};
其他人更短的递归,用参数来传partans. 省略了状态还原的代码,数字压入多次也采用了递归而不是循环
class Solution {
public:
void search(vector<int>& num, int next, vector<int>& pSol, int target, vector<vector<int> >& result)
{
if(target == )
{
result.push_back(pSol);
return;
}
if(next == num.size() || target - num[next] < )
return;
pSol.push_back(num[next]);
search(num, next, pSol, target - num[next], result);
pSol.pop_back();
search(num, next + , pSol, target, result);
}
vector<vector<int> > combinationSum(vector<int> &num, int target)
{
vector<vector<int> > result;
sort(num.begin(), num.end());
vector<int> pSol;
search(num, , pSol, target, result);
return result;
}
};
其他人动态规划的代码,还没看,速度并不快, 但很短
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector< vector< vector<int> > > combinations(target + , vector<vector<int>>());
combinations[].push_back(vector<int>());
for (auto& score : candidates)
for (int j = score; j <= target; j++){
auto sls = combinations[j - score];
if (sls.size() > ) {
for (auto& s : sls)
s.push_back(score);
combinations[j].insert(combinations[j].end(), sls.begin(), sls.end());
}
}
return combinations[target];
}
};
【leetcode】Combination Sum (middle)的更多相关文章
- 【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(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Combination Sum II (middle) ☆
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】Combination Sum II(组合总和 II)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...
- 【LeetCode】Combination Sum(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【Leetcode】【Medium】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- 防止双击选中html中文字
在开发过程中很常用的会给<span></span>等内联元素增加一个onlick事件,但是经常发生的一件事情就是点击的时候,选中了span中的字体:倒是不影响主逻辑,但是很难受 ...
- shell 字符截取
Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.aaa.com/123.htm. # 读sharp 谐音 杀: ${var#*//} 杀掉//左边的,保留右边的 ...
- Silverlight中嵌套html、swf、pdf
1.MainPage.xaml <UserControl x:Class="SilverlightClient.MainPage" xmlns="http://sc ...
- How to using x++ creating Vendors [AX2012]
.//Create party for the vendor public void createParty(VendorRequestCreate _vendorRequestCreate) { ; ...
- linux c 分解质因数
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...
- java之其它命令
java编译命令 javac: javac -d <目录> 源文件.java 指定存放生成的class文件的路径命令行下编译带包名的java源文件: javac -d . XX.java ...
- 从零单排学JavaWeb
之前是一个asp爱好者,感觉前途渺茫,特此转向Powerful的Java阵型,寻求心灵上的慰藉. 把自己遇到的问题记录下来,同时也分享给大家. 环境-下载 1 JDK http://dlsw.bai ...
- telnet 时代的 bbs
人类曾经用telnet 来访问 bbs,后来有了www,web 浏览器取代了telnet Telnet协议是TCP/IP协议族中的一员 arp和ping的区别 ping也属于一个通信协议,是TCP/I ...
- 【Go】为什么用go; Golang Erlang 前世今生
给自己一条退路,再次比较Erlang和Golang 2014-6-28 陈叶皓 chenyehao@gmail.com 雨天的周末,适合码字的时节... 一年前我开始学习go语言的时候,如获至宝,既有 ...
- 20145120 《Java程序设计》第2周学习总结
20145120 <Java程序设计>第2周学习总结 教材学习内容总结 因为前面有学习过C语言以及汇编语言,类型.运算符.流程控制等很多都是之前接触过的,因此在学习第三章的时候感觉并非十分 ...