LeetCode OJ--Combination Sum **
https://oj.leetcode.com/problems/combination-sum/
给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得7的所有组合。
递归,类似深搜的思想。
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > ans;
if(candidates.size()==)
return ans; //remove duplicates
sort(candidates.begin(),candidates.end());
vector<int>::iterator itr = unique(candidates.begin(),candidates.end());
candidates.resize(itr - candidates.begin());
if(candidates[]>target)
return ans; //remove the elements great than target
int len = candidates.size();
while(len>= && candidates[len-] > target)
len--;
candidates.resize(len); vector<int> ansPiece;
combinationSum(candidates,ans,target,,ansPiece); return ans;
}
void combinationSum(vector<int> & candidates,vector<vector<int> > &ans,int target,int pivot,vector<int> ansPiece)
{
if(target == )
{
ans.push_back(ansPiece);
return;
}
if( target < ||pivot == candidates.size())
return; int i = ;
while(target - i>=)
{
if(i != )
ansPiece.push_back(candidates[pivot]);
combinationSum(candidates,ans,target - i,pivot+,ansPiece); i = i + candidates[pivot];
}
}
};
LeetCode OJ--Combination Sum **的更多相关文章
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
- Leetcode 之 Combination Sum系列
39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...
随机推荐
- pip3 的安装 同时安装lxml和pygame
ubuntu18.04中 首先查看自己电脑的python版本,一般都会有2, 和3 python -V python3 -V 查看pip版本 pip -V pip3 -V 现在我们就可以开始安装我们的 ...
- Missian指南三:创建一个Missian服务器(使用spring)
在使用Missian时,spring是可选的,但是作者本人强烈推荐和Spring配合使用.Spring是一个伟大的项目,并且它不会对程序在运行时的效率带来任何损耗. Missian在服务器端依赖与Mi ...
- STM32位带操作
STM32的位带操作是基于cortex内核自带的,而不是st公司独创.基本的思路就是用一个32位的地址空间访问一个bit,因为stm32只支持32位数据的读取,不像51单片机一样,是可以单独对一位操作 ...
- MySQL的增、删、查、改操作命令
MySQL的增.删.查.改操作命令: 一.修改mysql数据库密码 格式:mysqladmin -u用户名 -p旧密码 password 新密码. 二.查看 查看多少个数据库:注意 后面带s #查看 ...
- docker 学习(2)
docker容器中安装vim ubuntu 中默认未装vim,docker run ubuntu vim 出现: container_linux.go:247: starting container ...
- PAT Advanced 1001
1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits mu ...
- HDU 6153 KMP
最终刷KMP目标就是为了挑战这道题!现在成功了恩... 首先,题目大意是:给出一个字符串str1,之后给出另一个字符串str2,问,str2的后缀在str1匹配的次数*后缀当前长度是多少 首先考虑正统 ...
- Sql日期时间格式转换(转 子夜.)
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- HDU 1535 S-Nim(SG函数)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 新博客 http://kunyashaw.com/
感谢博客园. 请关注我的新博客: http://kunyashaw.com/