leetcode 39 dfs leetcode 40 dfs
leetcode 39
先排序,然后dfs
注意先整全局变量可以减少空间利用
class Solution {
vector<vector<int>>ret;
vector<int>temp;
vector<int> srt;
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
srt=candidates;
sort(srt.begin(),srt.end());
dfs(target,0); //从下标0开始,杜绝0 1 ,1 0下这样的重复
return ret;
}
void dfs(int target,int index){
if(target==0)
{
ret.push_back(temp);
return;
}
for(int i=index;i<srt.size()&&(target-srt[i]>=0);i++)
{
temp.push_back(srt[i]);
dfs(target-srt[i],i);
temp.pop_back();
}
return;
}
};

leetcode 40
在39的基础上改了改
注意 vector的find是借助algorithm实现的
class Solution {
vector<vector<int>>ret;
vector<int>temp;
vector<int> srt;
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
srt=candidates;
sort(srt.begin(),srt.end());
dfs(target,0); //从下标0开始,杜绝0 1 ,1 0下这样的重复
return ret;
}
void dfs(int target,int index){
if(target==0)
{
//vector<vector<int>> :: itrator it;
if(find(ret.begin(),ret.end(),temp)==ret.end())
ret.push_back(temp);
return;
}
for(int i=index;i<srt.size()&&(target-srt[i]>=0);i++)
{
temp.push_back(srt[i]);
dfs(target-srt[i],i+1);
temp.pop_back();
}
return;
}
};

然后剪枝的话这样
class Solution {
vector<vector<int>>ret;
vector<int>temp;
int tp;
vector<int> srt;
int tar;
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
srt=candidates;
sort(srt.begin(),srt.end());
tar=target;
dfs(target,0); //从下标0开始,杜绝0 1 ,1 0下这样的重复
return ret;
}
void dfs(int target,int index){
if(target==0)
{
//vector<vector<int>> :: itrator it;
//if(find(ret.begin(),ret.end(),temp)==ret.end())
ret.push_back(temp);
return;
}
for(int i=index;i<srt.size()&&(target-srt[i]>=0);i++)
{
if(i>index&&srt[i]==srt[i-1]) //index表示的是当前target对应的,开始的位置,后续如果==index,对于同一个target会形成重复;若是不同target不会重复
continue;
temp.push_back(srt[i]);
dfs(target-srt[i],i+1);
temp.pop_back();
}
return;
}
};

leetcode 39 dfs leetcode 40 dfs的更多相关文章
- [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)
Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...
- Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves)
Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves) 深度优先搜索的解题详细介绍,点击 给出一个二维数组 A,每个单元格为 0(代表海)或 1( ...
- Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)
Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...
- Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)
Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...
随机推荐
- 全网最详细的PyCharm+Anaconda的安装。
目录 PyCharm的安装 一.下载安装包 1.安装网站 2.在导航栏输入网址回车 3.点击 DOWNLOAD. 4.它有专业版和社区版,我们下载社区版就可以使用了.(专业版要收费) 二.安装过程 5 ...
- 使用npm install安装项目依赖的时候报错
使用npm install安装项目依赖的时候报错: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.14.1 postin ...
- pyinstaller打包shotgun有关的程序
By 鬼猫猫 http://www.cnblogs.com/muyr/ 背景 使用pyinstaller打包跟shotgun有关的程序后,在自己电脑上运行都OK,但是编译好的exe在其他人的电脑上运行 ...
- Android事件分发机制四:学了事件分发有什么用?
" 学了事件分发,影响我CV大法吗?" " 影响我陪女朋友的时间" " ..... " 前言 Android事件分发机制已经来到第四篇了,在 ...
- SuperUpdate.sh 一键更换Linux软件源脚本
一.前言 有时候会遇到 Linux 的源更新速度非常的缓慢,特别是在国内使用默认的源,因为国内的网络环境,经常会出现无法更新,更新缓慢的情况.在这种情况下,更换一个更适合或者说更近,更快的软件源,会为 ...
- XShell下便捷上载/下载文件到虚拟机
1.客户机联网后,安装 rz,sz 服务,命令如下: yum install lrzsz 2.XShell连接客户机: 2.1 上传文件:运行rz,在弹窗内选择Windows本地文件上传到客户机当前目 ...
- join 查询优化
在开发中往往会出现查询多表联查的情况,那么就会用到 join 查询. Join查询种类 为了方便说明,先定义一个统一的表,下面再做例子. CREATE TABLE `t2` ( `id` int(11 ...
- git commit,启动文本编辑器
git commit中输入message的几种方式 - 简书 https://www.jianshu.com/p/ad461b99e860 在所有的git教程里,git commit肯定是一开始就会提 ...
- Enables DNS lookups on client IP addresses 域名的分层结构
虚拟域名访问,路由可以到达,但无输出. http://httpd.apache.org/docs/2.2/mod/core.html#hostnamelookups 移动解析 HttpDNS_域名解析 ...
- tee MultiWriter creates a writer that duplicates its writes to all the // provided writers, similar to the Unix tee(1) command.
https://zh.wikipedia.org/wiki/Tee 在计算机科学中,tee是一个常见的指令,它能够将某个指令的标准输出,导向.存入某个档案中.许多不同的命令行界面(Shell)都提供这 ...