LeetCode 039 Combination Sum
题目要求: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]
代码如下:
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(candidates.begin(), candidates.end());
vector<int>::iterator pos = unique(candidates.begin(), candidates.end());
candidates.erase(pos, candidates.end());
vector<vector<int> > ans;
vector<int> record;
searchAns(ans, record, candidates, target, 0);
return ans;
}
private:
void searchAns(vector<vector<int> > &ans, vector<int> &record, vector<int> &candidates, int target, int idx) {
if (target == 0) {
ans.push_back(record);
return;
}
if (idx == candidates.size() || candidates[idx] > target) {
return;
}
for (int i = target / candidates[idx]; i >= 0; i--) {
record.push_back(candidates[idx]);
}
for (int i = target / candidates[idx]; i >= 0; i--) {
record.pop_back();
searchAns(ans, record, candidates, target - i * candidates[idx], idx + 1);
}
}
};
LeetCode 039 Combination Sum的更多相关文章
- Java for LeetCode 039 Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
随机推荐
- Python3网络学习案例一:Ping详解
1. 使用Ping做什么 ping用于确定本地主机是否能与另一台主机成功交换(发送与接收)数据包,再根据返回的信息,就可以推断TCP/IP参数是否设置正确,以及运行是否正常.网络是否通畅等. 2. 效 ...
- MarkDown及Typora文本编辑器
文章介绍主要介绍MarkDown语法和与之能够配套使用的文本编辑器Typora的下载使用 1. MarkDown简介 MarkDown是一种纯文本标记语言,其书写与txt.word文档类似: 所有网站 ...
- Windows Server 2012 R2 辅助域控制器搭建
Windows Server 2012 R2 辅助域控制器搭建 以下操作都是基于主域已搭建成功的基础上,全程操作过程都是在辅域上操作完成. 地址 主域:10.228.81.207 辅域:10.228. ...
- XJOI 夏令营501-511NOIP训练18 高二学堂
在美丽的中山纪念中学中,有座高二学堂,同样也是因为一个人,让它们变 成了现在这个样子~那就是我们伟大的级主任.因为他,我们又迎来了一个木有电影,只有对答案的段考日:又迎来了一个不是大礼拜,而是小礼拜的 ...
- nginx vhost配置
server { listen 80; server_name crsdemo.my; index index.html index.htm index.php default.html defaul ...
- 一致性(ECMAScript语法标准翻译)
Conformance A conforming implementation of ECMAScript must provide and support all the types, values ...
- 淘宝客?CPS技术是怎么实现的?
前言 微信搜[Java3y]关注这个有梦想的男人,点赞关注是对我最大的支持! 文本已收录至我的GitHub:https://github.com/ZhongFuCheng3y/3y,有300多篇原创文 ...
- socket connect tcp_v4_connect
tcp_v4_connect /* This will initiate an outgoing connection. tcp_v4_connect函数初始化一个对外的连接请求,创建一个SYN包并发 ...
- Lamport面包店算法详解(转 侵删)
范例1: boolean choosing[n];表示进程是否在取号 int number[n];记录每个进程取到的号码 这些数据结构分别初始化为false和0,为了方便,定义如下符号: 若a&l ...
- mysql之分区表
1.分区表概述: 1.分区表的主要意义在于,对于表结构进行划分,不同的数据进入不同的分区中,以便于在查询过程中,只查找指定分区的数据,减少数据库扫描的数据量. 2.虽然从逻辑上看分区表是一张表,但是底 ...