【leetcode】Combination Sum
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]
利用递归搜索,同时剪枝就可以
class Solution { public: vector<vector<int> > combinationSum(vector<int> &candidates, int target) { sort(candidates.begin(),candidates.end()); vector<vector<int> > result; vector<int> tmp; backtracking(result,candidates,target,tmp,,); return result; } void backtracking(vector<vector<int> > &result,vector<int> &candidates, int &target,vector<int> tmp, int sum,int index) { if(sum==target) { result.push_back(tmp); return; } else { int sum0=sum; //注意index for(int i=index;i<candidates.size();i++) { tmp.push_back(candidates[i]); sum=sum0+candidates[i]; if(sum>target) { break; } backtracking(result,candidates,target,tmp,sum,i); tmp.pop_back(); } } } };
【leetcode】Combination Sum的更多相关文章
- 【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 III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Combination Sum (middle)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【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 ...
随机推荐
- [转]JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )
这两天看了一下深入浅出JVM这本书,推荐给高级的java程序员去看,对你了解JAVA的底层和运行机制有比较大的帮助.废话不想讲了.入主题: 先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(h ...
- Java常见异常
1. runtimeException子类: 1. java.lang.ArrayIndexOutOfBoundsException 数组索引越界异常.当对数组的索引值为负数或大于等于数组大小时 ...
- FastDFS在.Net平台上的使用
上一篇,了解了FastDFS是什么东东,一般稍微大一的网站都会做文件分离存储,FastDFS这轻型的分布式文件存储方式,非常有用. 此图片截取博友(张占岭)的勿喷 下面我们就了解一下,FastDFS在 ...
- 修改Oracle权限的SQL及常见错误
1.在cmd命令中进入sqlplus:相应的在DOS命令下执行:(1)set ORACLE_SID = $INSTANCE_NAME(2)sqlplus /nolog(3)connect user/p ...
- 【poj1088】 滑雪
http://poj.org/problem?id=1088 (题目链接) 题意 给出一个矩阵,任意选择一个起点,每次只能向周围4个格子中的值比当前格子小的格子移动,求最多能移动多少步. Soluti ...
- TYVJ P1403 [NOIP2010]关押罪犯
TYVJ的编译器总是要搞点岔子出来,上次是double必须用f输出而不能用lf,这次又不知道为何CE 于是去了洛谷P1525测试,AC 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1 ...
- Codeforces 650B Image Preview
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
- php多态设计
原文:http://www.cnblogs.com/tecs27/archive/2012/03/13/2394028.html 多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结 ...
- mysql 自连接
SELECT语句中的自连接. 到目前为止,我们连接的都是两张不同的表,那么能不能对一张表进行自我连接呢?答案是肯定的. 有没有必要对一张表进行自我连接呢?答案也是肯定的. 表的别名: 一张表可以自我连 ...