【LeetCode】040. Combination Sum II
题目:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
题解:
Solution 1 (TLE)
class Solution {
public:
void dfs(vector<vector<int>>& vv, vector<int>& v, vector<int> candidates, int target, int sum, vector<int>& visited) {
if(sum == target) {
vector<int>* tmp = new vector<int>;
*tmp = v;
sort((*tmp).begin(), (*tmp).end());
if(find(vv.begin(), vv.end(), *tmp) == vv.end())
vv.push_back(*tmp);
delete tmp;
return;
}
if(sum > target) return;
for(int i=; i<candidates.size(); ++i) {
if(visited[i] != ) continue;
v.push_back(candidates[i]);
visited[i] = ;
dfs(vv, v, candidates, target, sum+candidates[i], visited);
v.pop_back();
visited[i] = ;
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> vv;
vector<int> v;
vector<int> visited(candidates.size(),);
dfs(vv, v, candidates, target, , visited);
return vv;
}
};
Solution 1 中即使i从level开始遍历,也无法accepted,加入stop后才勉强通过,即Solution 2
Solution 2 (almost TLE but not 579ms)
class Solution {
public:
void dfs(vector<vector<int>>& vv, vector<int>& v, vector<int> candidates, int target, int sum, vector<int>& visited, int stop,int level) {
if(sum == target) {
vector<int>* tmp = new vector<int>;
*tmp = v;
sort((*tmp).begin(), (*tmp).end());
if(find(vv.begin(), vv.end(), *tmp) == vv.end())
vv.push_back(*tmp);
delete tmp;
return;
}
if(sum > target) {stop = ;return;}
for(int i=level; i<candidates.size(); ++i) {
if(visited[i] != ) continue;
v.push_back(candidates[i]);
visited[i] = ;
dfs(vv, v, candidates, target, sum+candidates[i], visited, stop, level+);
if(stop == ) {stop = ;break;}
v.pop_back();
visited[i] = ;
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> vv;
vector<int> v;
vector<int> visited(candidates.size(),);
sort(candidates.begin(), candidates.end());
dfs(vv, v, candidates, target, , visited, , );
return vv;
}
};
Solution 3 ()
class Solution {
public:
void dfs(vector<vector<int>>& vv, vector<int>& v, vector<int> candidates, int target, vector<int>& visited, int stop,int level) {
if(target == ) {
vector<int>* tmp = new vector<int>;
*tmp = v;
sort((*tmp).begin(), (*tmp).end());
if(find(vv.begin(), vv.end(), *tmp) == vv.end())
vv.push_back(*tmp);
delete tmp;
return;
}
if(target<) {stop = ;return;}
for(int i=level; i<candidates.size(); ++i) {
if(visited[i] != ) continue;
v.push_back(candidates[i]);
visited[i] = ;
dfs(vv, v, candidates, target-candidates[i], visited, stop,level+);
if(stop == ) {stop = ;break;}
v.pop_back();
visited[i] = ;
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> vv;
vector<int> v;
vector<int> visited(candidates.size(),);
sort(candidates.begin(), candidates.end());
dfs(vv, v, candidates, target, visited, ,);
return vv;
}
};
Solution 4
【LeetCode】040. Combination Sum II的更多相关文章
- 【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】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【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】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【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】113. Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
随机推荐
- Hibernate demo之使用注解
1.新建maven项目 testHibernate,pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...
- python创建迅雷批量任务
其实不是真的创建了批量任务,而是用python创建一个文本文件,每行一个要下载的链接,然后打开迅雷,复制文本文件的内容,迅雷监测到剪切板变化,弹出下载全部链接的对话框~~ 实际情况是这样的,因为用py ...
- linux支持的machine-types
在内核文件中arch/arm/tools/mach-types定义目前内核支持的板卡.芯片等: ##machine_is_xxx CONFIG_xxxx MACH_TYPE_xxx number ...
- vagrant 介绍,安装与使用
可以帮你统一团队成员的开发环境.如果你或者你的伙伴创建了一个Vagrantfile,那么你只需要执行vagrant up就行了,所有的软件都会安装并且配置好.团队成员可以通过相同的Vagrantfil ...
- 线程池 API (转)
文档原始地址 目录 线程池概述 线程池对象 回调环境对象 工作对象 等待对象 计时器对象 I/O 完成对象 使用清理组简化清理 回调实例 API 随着 Windows Vista® 的发布 ...
- YII框架学习(一)
1.安装: windows:将php命令所在的文件夹路径加入到环境变量中,通过cmd命令:进入yii框架中的framework目录,执行: php yiic webapp ../cms linux:类 ...
- 数据结构与算法之枚举(穷举)法 C++实现
枚举法的本质就是从全部候选答案中去搜索正确的解,使用该算法须要满足两个条件: 1.能够先确定候选答案的数量. 2.候选答案的范围在求解之前必须是一个确定的集合. 枚举是最简单.最基础.也是最没效率的算 ...
- ios Symbol(s) not found for architecture arm64总结 含隐藏错误cocoapods
一.通用 报错:Desktop/project/ASDF/WEIXIN/libWeChatSDK.a (3 slices) Undefinedsymbols for architecture arm6 ...
- 聚聚科技——php开发笔试题及答案
聚聚科技是一个刚创立的公司,很小很小,人很少,老板感觉是个典型的北京小伙儿,戾气很重,很有个性.笔试题倒是简单: 1. echo(), print(), print_r()的区别? echo是PHP语 ...
- socketserver源码解读
在看源码之前我们先来看看有关继承的知识(看源码就是在找继承关系----个人理解) 继承 : 我们先看上面的代码,这是一个简单的类继承,我们可以看到父类Base和子类Son,它们中各有一个Test ...