穷举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();
        }
    }
};
相关推荐
KaMeidebaby18 分钟前
卡梅德生物技术快报|bli亲和力检测gst:告别批量跑胶:BLI实时酶切监测技术加速GST融合蛋白下游流程优化
前端·网络·数据库·人工智能·算法
alphaTao39 分钟前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode
zhangfeng11331 小时前
Ascendc 大语言模型框架 AscendCraft 和pypto 的区别 pypto生成算子c++源码吗
c++·人工智能·语言模型·算子开发
一只小灿灿2 小时前
C++ 修饰符全面详解
开发语言·c++
code_pgf2 小时前
C/C++ 中 `typedef` 关键字详解
c语言·c++
会周易的程序员3 小时前
js-shm: 高性能 Node.js 共享内存模块
开发语言·javascript·c++·node.js·共享内存·shm
2023自学中3 小时前
imx6ull 开发板 贪吃蛇, C++11 SDL2 无硬件GPU优化版
linux·c++
anscos3 小时前
为CUDA 代码引入静态分析
c++·工业软件·功能检测
众少成多积小致巨3 小时前
C++ 规范参考(中)
c++
小保CPP4 小时前
OpenCV C++基于AI模型的场景文本识别(OCR)
c++·人工智能·opencv·计算机视觉·ocr