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. Hibernate各种主键生成器策略与配置详解(转载)

    http://www.cnblogs.com/kakafra/archive/2012/09/16/2687569.html 1.assigned 主键由外部程序负责生成,在 save() 之前必须指 ...

  2. ava如何实现系统监控、系统信息收集、sigar开源API的学习(转)

    ava如何实现系统监控.系统信息收集.sigar开源API的学习(转) 转自:http://liningjustsoso.iteye.com/blog/1254584 首先给大家介绍一个开源工具Sig ...

  3. paip.提升效率--gui 的选择--swing最佳实践swt awt

    paip.提升效率--gui 的选择--swing最佳实践swt awt ////////////////弹出消息框. ////////////////myeclipse swing 开发最佳实践.. ...

  4. Maven学习总结(四)——Maven核心概念——转载

    一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 ver ...

  5. Maven之打包时配置文件替换

    在JavaWeb项目中,使用maven打包.在打正式包时,需要手动修改数据库配置为线上环境的地址,这样每次修改起来比较麻烦. 搜索了一些资料后,大部分的做法或原理都是预先使用表达式占位符,然后在打包时 ...

  6. Help Viewer 2.2 独立运行

    "C:\Program Files (x86)\Microsoft Help Viewer\v2.2\HlpViewer.exe" /catalogName VisualStudi ...

  7. 内存中 OLTP - 常见的工作负荷模式和迁移注意事项(三)

    ----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<In-Memory OLTP – Comm ...

  8. centos下linux运行asp网站搭建配置-mono+nginx

    一.首先安装一些需要的软件包 1.  首先更新CentOS上的软件包:yum –y update. 2.  安装一些需要的库: yum -y install gcc gcc-c++ bison pkg ...

  9. iOS 9 failed for URL: "XXX://@" - error: "This app is not allowed to query for scheme XXX" iOS 从APP里启动另一APP

    iOS 从C APP里启动 D APP 首先在D APP里设置 URL Schemes 在info.plist 文件里添加URL Schemes URL Types -->item0 --> ...

  10. android中xmlns:tools属性详解

    今天读到一篇总结的非常棒的文章,写的逻辑很清晰也很实用,很少见到如此棒的文章了.就原文转发过来,我把格式给整理了一下,分享给园子里的各位朋友!好久没写博客了,就为2015年的11月留份纪念吧.希望对你 ...