[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]
#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 回溯的更多相关文章
- [Leetcode] Combination Sum 系列
Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
随机推荐
- Hibernate 的saveOrUpdate方法(转)
hibernate提供了saveOrUpdate的方法来进行数据库的操作.hibernate会根据对象的状态决定是insert还是update,其根本是通过xml文件中unsaved-value来确定 ...
- iOS-触摸事件、手势识别、摇晃事件、耳机线控
概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事件(手势操作).运动事件. ...
- Windows 8.1/Server 2012 R2/Embedded 8.1 with Update 3(MSDN最新版)
微软于12月16日更新了包含Update 3的ISO,此次更新并不会明显改善用户的界面体验,下载后请校验MD5.我整理了中.英文的8.1/服务器版/嵌入式版本/多国语言包. 1,Windows 8.1 ...
- Jenkins 插件 CIFS
Jenkis编译后我们往往需要把文件发布的其他的服务器上,典型的插件如下: Publish Over CIFS Plugin Publish Over FTP Plugin Publish ...
- linux服务器调整参数支持高并发
服务端调整系统的参数,在/etc/sysctl.conf中: ◦net.core.somaxconn = 2048◦net.core.rmem_default = 262144◦net.core.wm ...
- GBK、UTF8、UNICODE编码转换
string GBKToUTF8(const std::string& strGBK) { , strGBK.c_str(), -, NULL, ); WCHAR * wszUTF8 = ne ...
- iOS:个性化UITextView(缩进,行距,铺满)
总体来说个性化定制UITextView中的内容有两种方法: 1,从文件中读取内容到UITextView,这个个人感觉使用rtfd和rtf格式文件效果非常好. 2,使用NSAttributeString ...
- Transaction recovery: lock conflict caught and ignored
Transaction recovery: lock conflict caught and ignored环境:RAC 4节点.oracle 11.2.0.4.redhat 5.9 64bit 问题 ...
- EXCELL中怎么将两列数据对比,找出相同的和不同的数据?
假设你要从B列中找出A列里没有的数据,那你就在C1单元格里输入“=IF(ISNA(VLOOKUP(B1,A:A,1,0)),"F","T")”显示T就表示有,F ...
- 《objective-c基础教程》学习笔记(八)—— 拆分接口和实现
在之前的项目中,我们编程都是直接写在一个main.m文件中.类的main()函数,@interface和@implementation部分都塞进一个文件.这种结构对于小程序和简便应用来说还可以.但是项 ...