题目:

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. Apc缓存Opcode(转)

    1.PHP执行 PHP的运行阶段也分成三个阶段: Parse.语法分析阶段. Compile.编译产出opcode中间码. Execute.运行,动态运行进行输出.                   ...

  2. CentOS6.5下Oracle11G-R2安装、卸载

    CentOS6.5下Oracle11G-R2安装.卸载 资源下载地址(包含本人全部安装过程中,系统备份文件):http://download.csdn.net/detail/attagain/7700 ...

  3. python学习(八)阶段性总结

    这里学习了python的一点点基础然后来总结一下 python是一个强类型的语言 数字: 数字的类型大概有:整数.浮点数.复数.固定精度的十进制数.带分子和分母的有理数 数字与字符串之间不能直接相加, ...

  4. python学习(三)数字类型示例

    奶奶的报了这个错,我以为可以像java中字符串加数字的嘛 Traceback (most recent call last):   File "./number.py", line ...

  5. Can a GridView have a footer and header just like ListView?

    Aquick question: In ListView I use this code: list.addHeaderView(headerView); How to deal with it wh ...

  6. python 基础 8.2 编译正则对象

    #/usr/bin/python #coding=utf-8 #@Time   :2017/11/14 9:55 #@Auther :liuzhenchuan #@File   :编译正则对象.py ...

  7. python venv虚拟环境

    1 目的 给python应用一个独立的运行环境,独立于其它的python应用也独立于系统的python环境. 环境升级不影响其它应用. 避免包冲突. 2 创建方式 2.1 pycharm中创建 pro ...

  8. DSP/BIOS使用之初窥门径——滴答时钟及烧写Flash

    操作平台和环境 DSP型号:TMS320C6713 仿真器:XDS510PLUS Flash型号:AM29LV800BT或AM29LV800BT都试过(一般接口一样,区别不大) RAM型号:MT48L ...

  9. No provisioned iOS devices are available with a compatible iOS version. Connect an iOS device with a

    No provisioned iOS devices are available with a compatible iOS version. Connect an iOS device with a ...

  10. PHP加Nginx实现动态裁剪图片方案

    许久以前写过一篇也是关于高性能PHP图片动态裁剪方案的文章,那文章使用的是nginx Cache和rewrite实现的,当然再加上CDN,那个方案存在一个问题就是图片并没有实际生成,而是以二进制的形式 ...