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 ...
随机推荐
- ping 命令示例
将下面的代码粘贴到记事本中,然后保存为扩展名为BAT的文件,运行就可以将网段内ping不通的IP地址写入到文本文件IP.txt中. @echo offsetlocal ENABLEDELAYEDEXP ...
- php压缩文件夹并下载到本地
/** * @param $path 要压缩的文件夹路径 * @param $filename 要生成的压缩包名称 */ public function create_zip($path,$filen ...
- [Ceoi2004]Journey
题目描述 给出N个点,及你的出发点K. 接下来N-1行描述有关边的开始点,结束点,边长.保证图中不会有环 接下来给出数字J,代表你要走多少个点. 接下来J个数字,代表你要走过的点的编号.当然你可以自己 ...
- Java运算符概要与数学函数
运算符概要 在Java中,使用算术运算符+,-,*,/表示加减乘除运算,当参与/的运算的两个操作数都是整数时,表示整数除法,否则,表示浮点除法.整数的求余操作(有时称为取模),用%表示,例如,15/2 ...
- file转化为binary对象发送给后台
具体代码如下: function filechange(e) { var file = $('#filed').get(0).files[0]; var fileSize = file.size, f ...
- vue、element-ui 后台菜单切换重新请求数据
我们在做后台管理系统时,通常将数据请求挂载到created或mounted钩子中,但这样引发的问题是它只会被出发一次,如果不同菜单中数据关联性较大,切换过程中未及时更新数据,容易引发一些问题,这种情况 ...
- 笔记 | 吴恩达新书《Machine Learning Yearning》
这本书共112页,内容不多,偏向于工程向,有很多不错的细节,在此记录一下. 0 书籍获取 关注微信公众号"机器学习炼丹术",回复[MLY]获取pdf 1 测试集与训练集的比例 2 ...
- 小白都看得懂的Javadoc使用教程
Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...
- (07)-Python3之--函数
1.定义 函数:实现了某一特定功能. 可以重复使用. 例如: len() 功能:获取长度.input() 功能: 控制台输入print() 功能:输出 语法: def 函数名称(参数 ...
- CentOS安装mysql、JDK、Tomcat部署环境
1.1. 安装mysql-5.6 1.1.1. 检测系统内部有没有安装其他的mysql数据库 $ rpm -qa | grep mysql 1.1.2. 如果内部有需要先删除Mysql $ yum r ...