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的更多相关文章

  1. LeetCode_Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  3. 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 ...

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. 杜教的AAA树

    膜膜膜,常数挺小的... #include<iostream> #include<cstdio> #include<cmath> #include<algor ...

  2. Contest - 第10届“新秀杯”ACM程序设计大赛网络预选赛 赛后信息(晋级名单)

    经过比赛结果以及综合评定,以下42名同学暂定出现.下为出现名单(打*为 友情参赛 或为 有重大作弊嫌疑的选手). 在即日24时之前,若有异议,仍可申诉,申诉邮箱:desgard_duan@foxmai ...

  3. openstack 手动安装版 功能测试

    nova network-create demo-net --bridge br100 --multi-host T --gateway 192.168.3.252 --dns1 202.102.19 ...

  4. 警告: 隐式声明与内建函数‘exit’不兼容 [默认启用]

    警告: 隐式声明与内建函数‘exit’不兼容 [默认启用] 最近在学习linux下的多任务编程,用到exit等函数时,经常出现该警告,查找资料后发现,原因其实很简单,没有把stdlib.h头文件包含进 ...

  5. WPF Customize TabControl

    有篇很好的文章 http://www.blogs.intuidev.com/post/2010/01/25/TabControlStyling_PartOne.aspx 详细介绍了如何Customiz ...

  6. java foreach记录

    实现原理解释: http://blog.csdn.net/a596620989/article/details/6930479 http://stackoverflow.com/questions/8 ...

  7. 听说每天都要写随笔,word哥~

    今天主要学习了html的基本知识,进制的转换,无序列表,有序列表和表格,都是很基本的东西,然后自己自习了表单. <!DOCTYPE html PUBLIC "-//W3C//DTD X ...

  8. [实战]挖掘CSRF姿势

    [-]CSRF是个什么鬼? |___简单的理解: |----攻击者盗用了你的身份,以你的名义进行某些非法操作.CSRF能够使用你的账户发送邮件,获取你的敏感信息,甚至盗走你的财产. |___CSRF攻 ...

  9. javascript 中的location.reload

    location.reload()是什么意思 location.reload() 括号内有一个参数 true/false , 为空和false的效果一样. 如果该方法没有规定参数,或者参数是 fals ...

  10. OpenSSL之PKey的EVP封装

    在Openssl中,非对称加密涉及到两个密钥.一个为公开的密钥(公钥),一个为非公开的密钥.而OpenSSL中非对称加密算法有RSA.DSA.ECC,他们的原理不同,因此其密钥结构不同.下面我们列出我 ...