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]

Hide Tags

Array Backtracking

 

 
   一道回溯题目,可以剪枝提升速度。
 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public:
vector<vector<int > > ret;
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<int > stk;
ret.clear();
vector<int > tmp(candidates.begin(),candidates.end());
sort(tmp.begin(),tmp.end());
helpFun(tmp,target,,stk);
return ret;
} void helpFun(vector<int> & cand,int tar, int idx,vector<int > & stk)
{
if(tar<) return ;
if(tar==){
ret.push_back(stk);
return ;
}
if(idx==cand.size()) return;
stk.push_back(cand[idx]);
helpFun(cand,tar-cand[idx],idx,stk);
stk.pop_back();
helpFun(cand,tar,idx+,stk);
}
}; int main()
{
vector<int > cand = {,,,};
Solution sol;
vector<vector<int > > ret=sol.combinationSum(cand,);
for(int i =;i<ret.size();i++){
for(int j=;j<ret[i].size();j++)
cout<<ret[i][j]<<" ";
cout<<endl;
}
return ;
}

[LeetCode] Combination Sum 回溯的更多相关文章

  1. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  2. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. [LeetCode] Combination Sum II 组合之和之二

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

  5. [LeetCode] Combination Sum 组合之和

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

  6. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  7. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  8. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  9. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

随机推荐

  1. 用distinct在MySQL中查询多条不重复记录值[转]

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...

  2. MySQL联合查询语法内联、左联、右联、全联

    MySQL联合查询效率较高,以下例子来说明联合查询(内联.左联.右联.全联)的好处: T1表结构(用户id,用户名,密码)   userid   username  password 1   jack ...

  3. 【设计模式】Java版设计模式的类图汇总

    Abstract Factory Intent: Provide an interface for creating families of related or dependent objects ...

  4. Atitit.如何选择技术职业方向

    Atitit.如何选择技术职业方向 1. 原则是应该如下的应该从以下指标判断1 1.1. 技术的长寿性(长生命周期1 1.2. 技术的普适性(市场份额)1 1.3. **属于open体系还是封闭体系? ...

  5. atitit.微信项目开发效率慢的一些总结

    atitit.微信项目开发效率慢的一些总结 #---理念问题..这个是最大的问题.. 要有专人提升开发效率才好.. #---没有一个好的开发方法体系.... ini deve 法. fell asd+ ...

  6. paip.输出内容替换在Apache 过滤器filter的设置

    paip.输出内容替换在Apache 过滤器filter的设置 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...

  7. Java集合——题目

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

  8. jQuery实现左移右移

    <html> <head> <meta charset="utf-8"> <title>完成左移右移</title> & ...

  9. JAVA编程中的类和对象

    1:初学JAVA,都知道JAVA是面向对象的编程.笔者这节开始说说类和对象.(实例仅供参考,如若复制粘贴记得修改包名和类名,避免出错) 学习JAVA的快捷键,Alt+/代码补全功能,其实此快捷键启动了 ...

  10. Python面试题(一)

    **晚上在公司的论坛上看到一道面试题,题目如下:随机给定一字符串和字符,要求重排,比如:'abde','c'.重排之后变成'abcde' **看到他们给的答案很多都是二分法重排,既然是字符类的处理,当 ...