【Lintcode】135.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.
Example
Given candidate set [2,3,6,7] and target 7, a solution set is:
[7]
[2, 2, 3]
题解:
这个题和LeetCode不一样,这个允许重复数字产生,那么输入[2,2,3],7 结果为[2, 2, 3], [2, 2, 3], [2, 2, 3];要么对最后结果去除重复数组,要么在处理之前就对candidates数组去重复,或者利用set不重复的特点添加数组。
Solution 1 ()
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
if (candidates.empty()) {
return {{}};
}
set<vector<int> > res;
vector<int> cur;
sort(candidates.begin(), candidates.end());
dfs(res, cur, candidates, target, );
return vector<vector<int> > (res.begin(), res.end());
}
void dfs(set<vector<int> > &res, vector<int> &cur, vector<int> candidates, int target, int pos) {
if (!cur.empty() && target == ) {
res.insert(cur);
return;
}
for (int i = pos; i < candidates.size(); ++i) {
if (target - candidates[i] >= ) {
cur.push_back(candidates[i]);
dfs(res, cur, candidates, target - candidates[i], i);
cur.pop_back();
} else {
break;
}
}
}
};
【Lintcode】135.Combination Sum的更多相关文章
- 【Lintcode】153.Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】039. Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】216. Combination Sum III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...
随机推荐
- 并发错误:事务(进程 ID )与另一个进程已被死锁在 lock 资源上,且该事务已被选作死锁牺牲品
这个是并发情况下导致的数据库事务错误,先介绍下背景. 背景 springboot+springmvc+sqlserver+mybatis 一个controller里有五六个接口,这些接口都用到了spr ...
- HDU1845Jimmy’s Assignment(无向图,最大匹配)
题意:就是求最大匹配 #include<cstdio> #include<iostream> #include<algorithm> #include<cma ...
- Spring Boot从入门到实战:整合Web项目常用功能
在Web应用开发过程中,一般都涵盖一些常用功能的实现,如数据库访问.异常处理.消息队列.缓存服务.OSS服务,以及接口日志配置,接口文档生成等.如果每个项目都来一套,则既费力又难以维护.可以通过Spr ...
- HTML5 2D平台游戏开发#4状态机
在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...
- Spark源码分析之四:Stage提交
各位看官,上一篇<Spark源码分析之Stage划分>详细讲述了Spark中Stage的划分,下面,我们进入第三个阶段--Stage提交. Stage提交阶段的主要目的就一个,就是将每个S ...
- Windows下安装Oracle 11g全过程。
1. 解压Oracle11.1.0.6 for win32,然后点击setup 2.选择高级安装,下一步 3.选择企业版,下一步 4.修改Oracle基目录,也可以是默认,下一步 5.将状态复选框打上 ...
- KEIL下分散加载文件的使用(zt)
KEIL下分散加载文件的使用 对于分散加载的概念,在<ARM体系结构与编程>书中第11章有明确介绍. 分散加载文件(即scatter file 后缀为.scf)是一个文本文件,通过编写 ...
- ios - UISearchBar输入框背景色
//输入框背景色 bar.searchBarStyle = UISearchBarStyleMinimal; [bar positionAdjustmentForSearchBarIcon:UISea ...
- Matlab时频图
[b,f,t]=specgram(data,nfft,Fs,window,numoverlap); imagesc(t,f,20*log10(abs(b))), axis xy, colormap(j ...
- spring boot 项目搭建时,各个依赖的作用
项目搭建页面 https://start.spring.io/ 各个依赖的作用 List of dependencies for Spring Boot 2.1.5.RELEASE Core DevT ...