LeetCode_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:
void DFS(vector<int> &candidates, int target, int start, int sum, vector<int> &tp){
if(sum == target){
res.push_back(tp);
return;
}
for(int i = start; i< candidates.size(); ++i){
if(candidates[i] + sum <= target){
tp.push_back(candidates[i]);
DFS(candidates, target, i, sum+candidates[i], tp);
tp.pop_back();
}
}
}
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
int len = candidates.size();
if(len < 1 || target <1) return res;
sort(candidates.begin(), candidates.end());
vector<int> tp;
DFS(candidates, target, 0, 0, tp);
return res; }
private:
vector<vector<int>> res;
};
LeetCode_Combination Sum的更多相关文章
- LeetCode_Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- 生成excel内存溢出问题的解决方式
常用的excel生成工具包括jxl.poi.但二者都存在一个问题:生成excel需要大量的消耗内存.如果一次性往excel中写入的数据足够的多将导致内存溢出. 1.数据写入excel为什么会大量的消耗 ...
- Andoid实现手动绘图
public class MainActivity extends Activity { int width,height; private GameView gameview; private Ca ...
- 为兴趣求职:如何学习UI框架,请将你的看法观点写在评论下面
前言:此篇文章是就我女朋友的求职和前端学习经历而写,希望得到UI前辈的热心指点,不胜感激涕零! 地理坐标: 中国,四川,成都 求职经历: 她之前找过两份工作,第一份是金融销售专员,第二份是汽车保险.她 ...
- Codeforces 460 DE 两道题
D Little Victor and Set 题目链接 构造的好题.表示是看了题解才会做的. 假如[l,r]长度不超过4,直接暴力就行了. 假如[l,r]长度大于等于5,那么如果k = 1,显然答案 ...
- [Python]round四舍五入精度缺失的解决
环境: os: win7 64bit python:2.7.5 32bit 对python四舍五入的解决方案 现象: 一般的四舍五入操作都是使用内置的round方法 In [14]: round ...
- JS-Array数组内置对象
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- java反射--注解的定义与运用以及权限拦截
自定义注解类编写的一些规则: 1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是 ...
- ListView OnItemClickListener position 索引不正确
在使用ListView添加如下代码时 listview.setOnItemClickListener(new OnItemClickListener() { @Override public void ...
- 差别client、offset、scroll系列以及event的几个距离属性
element元素结点属性 一. offset系列 1.offsetWidth 和offsetHeight element.offsetWidth是一个仅仅读属性,它包含了: css width + ...
- 【转】Linux中安装Resin
安装步骤: Ø 安装resin前先要保证安装了JDK,可以用命令查看是否安装了JDK: [root@wxr webapps]# java -versions java version " ...