穷举vs暴搜vs深搜vs回溯vs剪枝(二)

文章目录

括号生成

题目:括号生成


思路

何为有效的括号:
1.左括号数量=右括号数量
2.从头开始的任意子串满足,左括号数量 ≥ 有括号数量

  • 当左括号数小于对数时,我们可以继续添加左括号;
  • 当有括号数小于左括号数时,我们可以继续添加右括号;
  • 递归出口,右括号数等于对数时;

C++代码

cpp 复制代码
class Solution 
{
    int l_count, r_count, n;
    string path;
    vector<string> ret;
public:
    vector<string> generateParenthesis(int _n) 
    {
        n = _n;
        dfs();
        return ret;
    }
    void dfs()
    {
        if(r_count == n )
        {
            ret.push_back(path);
            return;
        }
        if(l_count < n )
        {
            path.push_back('(');
            l_count++;
            dfs();
            path.pop_back();
            l_count--;
        }
        if(r_count < l_count)
        {
            path.push_back(')');
            r_count++;
            dfs();
            path.pop_back();
            r_count--;
        }
    }
};

组合

题目:组合


思路

从1到n中选择k个数的所有组合

  • dfs(int pos)每次将当前位置加入path,并从下一个位置开始递归(剪支)(为了避免重复结果)
  • 当一次结果path长度等于k时,将其加入到答案中;

C++代码

cpp 复制代码
class Solution 
{
    vector<vector<int>> ret;
    vector<int> path;
    int n, k;
public:
    vector<vector<int>> combine(int _n, int _k) 
    {
        n = _n;
        k = _k;
        dfs(1);
        return ret;
    }
    void dfs(int pos)
    {
        if(path.size() == k)
        {
            ret.push_back(path);
            return;
        }
        for(int i = pos; i <= n; i++)
        {
            path.push_back(i);
            dfs(i + 1);
            path.pop_back();
        }
    }
};

目标和

题目:目标和


思路

从左往右依次操作每个数的符号,要么加号要么减号

C++代码

cpp 复制代码
class Solution 
{
    int ret;
    int path;
    int target;
public:
    int findTargetSumWays(vector<int>& nums, int _target) 
    {
        target = _target;
        dfs(nums, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos)
    {
        if(pos == nums.size())
        {
            if(path == target) ret++;
            return;
        }


        path += nums[pos];
        dfs(nums, pos + 1);
        path -= nums[pos];
        
        path -= nums[pos];
        dfs(nums, pos + 1);
        path += nums[pos];
    }
};

但我们发现这段代码耗时有点多,这是因为我们将path放在了全局变量;下面代码我们将其放在dfs()函数的参数中

cpp 复制代码
class Solution 
{
    int ret;
    int target;
public:
    int findTargetSumWays(vector<int>& nums, int _target) 
    {
        target = _target;
        dfs(nums, 0, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos, int path)
    {
        if(pos == nums.size())
        {
            if(path == target) ret++;
            return;
        }
        dfs(nums, pos + 1, path + nums[pos]);
 
        dfs(nums, pos + 1, path - nums[pos]);
    }
};
  • 当一个结果是整形时,我们最好将其放在dfs()函数的参数中;
  • 因为整形的加减法也是耗时的;
  • 当一个结果是保存在数组中时,我们最好将其设置为全局变量;
  • 因为每次递归拷贝vector也是消耗大的

组合总和

题目:组合总和


思路

上一层选了什么,当前层就从该位置开始选择

  • 剪支:当前结果等于目标值时,直接返回,并将其加入答案数组中;
  • 当前结果大于于目标值时,直接返回;
  • 选完还没有得到结果,直接返回;

C++代码

cpp 复制代码
class Solution 
{
    vector<vector<int>> ret;
    vector<int> path;
    int target;
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int _target) 
    {
        target = _target;
        dfs(candidates, 0, 0);
        return ret;
    }
    void dfs(vector<int>& candidates, int pos, int sum)
    {
        if(sum == target)
        {
            ret.push_back(path);
            return;
        }
        else if(sum > target || pos == candidates.size())
        {
            return;
        }

        for(int i = pos; i < candidates.size(); i++)
        {
            path.push_back(candidates[i]);
            dfs(candidates, i, sum + candidates[i]);
            path.pop_back();
        }
    }
};
相关推荐
Matlab程序猿小助手5 分钟前
【MATLAB源码-第303期】基于matlab的蒲公英优化算法(DO)机器人栅格路径规划,输出做短路径图和适应度曲线.
开发语言·算法·matlab·机器人·kmeans
CoderIsArt11 分钟前
CORDIC三角计算技术
人工智能·算法·机器学习
立志成为大牛的小牛12 分钟前
数据结构——二十九、图的广度优先遍历(BFS)(王道408)
数据结构·数据库·学习·程序人生·考研·算法·宽度优先
shark_dev12 分钟前
C++新特性——正则表达式
c++
Alex艾力的IT数字空间14 分钟前
基于PyTorch和CuPy的GPU并行化遗传算法实现
数据结构·人工智能·pytorch·python·深度学习·算法·机器学习
云知谷18 分钟前
【经典书籍】C++ Primer 第16章模板与泛型编程精华讲解
c语言·开发语言·c++·软件工程·团队开发
屁股割了还要学24 分钟前
【Linux入门】常用工具:yum、vim
linux·运维·服务器·c语言·c++·学习·考研
仰泳的熊猫25 分钟前
LeetCode:51. N 皇后
数据结构·c++·算法·leetcode
独自破碎E27 分钟前
LeetCode 381: O(1) 时间插入、删除和获取随机元素 - 允许重复
java·算法·leetcode
Miraitowa_cheems1 小时前
LeetCode算法日记 - Day 81: 最大子数组和
java·数据结构·算法·leetcode·决策树·职场和发展·深度优先