[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 ...
随机推荐
- paip.语义分析--单字词名词表
paip.语义分析--单字名词表 INSERT INTO t (word) SELECT DISTINCT word FROM `word_main` where tsisin is not n ...
- iOS开发——高级技术&蓝牙服务
蓝牙服务 蓝牙 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的 ...
- C#中使用DES和AES加密解密
C#中使用DES和AES加密解密 2008-01-12 09:37 using System;using System.Text;using System.Security.Cryptography; ...
- P2P的原理和常见的实现方式(为libjingle开路)
参考原文 为了项目的IM应用,最近在研究libjingle,中间看了也收集了很多资料,感慨网上很多资料要么太过于纠结协议(如STUN.ICE等)实现细节,要么中间有很多纰漏.最后去伪存真,归纳总结了一 ...
- android: Android Notification
Notification即通知,用于在通知栏显示提示信息. 在较新的版本中(API level > 11),Notification类中的一些方法被Android声明deprecated(弃用 ...
- maven继承parent,relativePath warn信息的解决办法
往下看之前一定要先看 %MAVEN_HOME%/conf/settings.xml 配置文件的是否更改了,是否配置正确 <mirror> <id>nexus</id> ...
- 服务器跟VPS有什么区别
你好. 服务器是独立的真实存在的硬件设备.其实也就是一台高端电脑.他是放在机房运行的.主要为网站以及一些软件应用提供运行平台.而VPS是虚拟服务器.他是利用软件在服务器上虚拟出来的.也就是分配出一部分 ...
- Android 图片的裁剪与相机调用
有时候我们需要的图片并不适合我们想要的大小, 那么我们就可以用到系统自带的图片裁剪功能, 把规定范围的图像给剪出来. 贴上部分代码: //调用图库 Intent intent = new Intent ...
- 用于主题检测的临时日志(b2d5c7b3-e3f6-4b0f-bfa4-a08e923eda9b - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
这是一个未删除的临时日志.请手动删除它.(1c773d57-4f35-40cf-ad62-bd757d5fcfae - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
- RPC 135端口