博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode】040. Combination Sum II
阅读量:5037 次
发布时间:2019-06-12

本文共 2799 字,大约阅读时间需要 9 分钟。

题目:

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
>& vv, vector
& v, vector
candidates, int target, int sum, vector
& visited) { if(sum == target) { vector
* tmp = new vector
; *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=0; i
> combinationSum2(vector
& candidates, int target) { vector
> vv; vector
v; vector
visited(candidates.size(),0); dfs(vv, v, candidates, target, 0, visited); return vv; }};

  Solution 1 中即使i从level开始遍历,也无法accepted,加入stop后才勉强通过,即Solution 2 

Solution 2 (almost TLE but not 579ms)

class Solution {public:    void dfs(vector
>& vv, vector
& v, vector
candidates, int target, int sum, vector
& visited, int stop,int level) { if(sum == target) { vector
* tmp = new vector
; *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 = 1;return;} for(int i=level; i
> combinationSum2(vector
& candidates, int target) { vector
> vv; vector
v; vector
visited(candidates.size(),0); sort(candidates.begin(), candidates.end()); dfs(vv, v, candidates, target, 0, visited, 0, 0); return vv; }};

 

Solution 3 ()

class Solution {public:    void dfs(vector
>& vv, vector
& v, vector
candidates, int target, vector
& visited, int stop,int level) { if(target == 0) { vector
* tmp = new vector
; *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<0) {stop = 1;return;} for(int i=level; i
> combinationSum2(vector
& candidates, int target) { vector
> vv; vector
v; vector
visited(candidates.size(),0); sort(candidates.begin(), candidates.end()); dfs(vv, v, candidates, target, visited, 0,0); return vv; }};

Solution 4 

 

转载于:https://www.cnblogs.com/Atanisi/p/6789004.html

你可能感兴趣的文章
Centos下PHP7.1打开Oracle扩展
查看>>
变量、初始化块和构造方法的初始化顺序问题(笔试题)
查看>>
前端 ---- js 模拟百度导航栏滚动案例
查看>>
jQuery上传文件按钮美化
查看>>
Vue安装Element 的详细步骤
查看>>
用dwr封装表单项提交表单
查看>>
Redis 哈希(Hash)
查看>>
树的统计 树链剖分
查看>>
android,微信,人人,<android 无标题栏 >微博开机加载一幅图片,再跳转到主应用的实现...
查看>>
css选择器
查看>>
python里面的xlrd模块详解以及样例
查看>>
python系统学习:第三周之文件操作
查看>>
深入理解IEnumerable和IQueryable两接口的区别
查看>>
Code First is a bad name,这些年我们对Code First的理解都错了 !很震惊吧?
查看>>
iOS 字符串的UTF8 编码 以及归档反归档
查看>>
hive (一) ----- hive的安装和使用
查看>>
最新最准确各大搜索引擎蜘蛛名称2014-4-15 10:02:52
查看>>
对象哈希码
查看>>
数组及其练习题
查看>>
MyEclipse 及Tomcate 安装 配置
查看>>