题目:

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的更多相关文章

  1. 【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 ...

  2. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  3. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  4. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  5. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  6. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  7. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  8. 【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 ...

  9. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

随机推荐

  1. windows下使用Eclipse编译执行MapReduce程序 Hadoop2.6.0/Ubuntu

    一.环境介绍 宿主机:windows8 虚拟机:Ubuntu14.04 hadoop2.6伪分布:搭建教程http://blog.csdn.net/gamer_gyt/article/details/ ...

  2. hdu 2063过山车

    二分匹配 #include<iostream> #include<cstring> using namespace std; int k,m,n; int rem[500+5] ...

  3. Npm 被公司墙解决方法

    npm被公司墙了,不能用npm安装任何包应用了. npm ERR! Darwin npm ERR! argv "/usr/local/Cellar/node/6.4.0/bin/node&q ...

  4. JavaScrip函数与声明表达式

    首先我们看下函数的两种命名方式 1.函数声明,声明一个函数 function test1(){ var a=0; console.log(a); //左一些操作... } 执行结果如下 我们看一下,无 ...

  5. scikit-learn:4. 数据集预处理(clean数据、reduce降维、expand增维、generate特征提取)

    本文參考:http://scikit-learn.org/stable/data_transforms.html 本篇主要讲数据预处理,包含四部分: 数据清洗.数据降维(PCA类).数据增维(Kern ...

  6. iPhone与iPad开发实战读书笔记

    iPhone开发一些读书笔记 手机应用分类1.教育工具2.生活工具3.社交应用4.定位工具5.游戏6.报纸和杂志的阅读器7.移动办公应用8.财经工具9.手机购物应用10.风景区相关应用11.旅游相关的 ...

  7. String知识点

  8. 九度OJ 1006:ZOJ问题 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18621 解决:3197 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. ...

  9. mysql insert返回主键

    使用mybatis的话,很方便. 使用useGeneratedKeys和keyProperty,keyProperty是插入的java对象的属性名,不是表的字段名. 这样,在插入该条记录之后,生成的主 ...

  10. php标准库DirectoryIterator类的操作说明

    <?php $dir = new DirectoryIterator(dirname(__FILE__)); foreach ($dir as $fileInfo) { if ($fileInf ...