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

  1. [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)

    39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...

  2. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  3. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

  4. [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 ...

  5. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  6. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

  7. Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves)

    Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves) 深度优先搜索的解题详细介绍,点击 给出一个二维数组 A,每个单元格为 0(代表海)或 1( ...

  8. Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)

    Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...

  9. Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)

    Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...

随机推荐

  1. Netty学习:伪共享

    目录 Netty中的伪共享 伪共享的原理以及介绍 Netty中的伪共享 先说为什么知道这个概念吧,期初看Netty源码的时候,看到了NioEventLoop的构建,其中有这么一句代码: private ...

  2. 《进击吧!Blazor!》第一章 2.Hello Blazor

    第二次写专栏,开头还是不知道说什么,所以--先来段广告<进击吧!Blazor!>是本人与张善友老师合作的Blazor零基础入门系列视频,此系列能让一个从未接触过Blazor的程序员掌握开发 ...

  3. python工业互联网应用实战3—Django Admin列表

    Django Admin笔者使用下来可以说是Django框架的开发利器,业务model构建完成后,我们就能快速的构建一个增删查改的后台管理框架.对于大量的企业管理业务开发来说,可以快速的构建一个可发布 ...

  4. JavaScript中原型对象的应用!

    JavaScript中原型对象的应用! 扩展内置对象的方法 我以数组对象为例! // 原型对象的应用 扩展内置对象方法! Array.prototype.sum = function() { var ...

  5. Hash Join: Basic Steps

    Joins https://docs.oracle.com/database/121/TGSQL/tgsql_join.htm#TGSQL242 tidb/index_lookup_hash_join ...

  6. grpc-metadata

    https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md https://github.com/grpc/g ...

  7. # from tall import b from tall import * print(b) __all__ 模块 引用管理

    ├── __init__.py├── tall2.py└── tall.pytall.pya = 23b = 34class I: def __init__(self): print(444)clas ...

  8. RAID系统被初始化

    RAID系统被初始化 https://forum.huawei.com/enterprise/zh/thread-256077-1-1.html

  9. 从零开始学Java (二)Hello

    1.新建Hello.java文件,写入以下内容 1 public class Hello { 2 public static void main(String[] args) { 3 System.o ...

  10. JavaScript基础知识-基本概念

    typeof操作符 typeof 操作符返回一个字符串,表示未经计算的操作数的类型. // 数值 typeof 37 === 'number'; typeof 3.14 === 'number'; t ...