leetcode算法题之递归--综合练习(一)

news/2024/7/20 22:34:49 标签: 算法, leetcode, 深度优先

此专题对我们之前所学的关于递归的内容进行一个整合,大家可以自行练习,提升自己的编码能力。

本章目录

  • 1.找出所有子集的异或总和在求和
  • 2.全排列II
  • 3.电话号码的字母组合
  • 4.括号生成
  • 5.组合
  • 6.目标和
  • 7.组合总和
  • 8.字母大小写全排列
  • 9.优美的排列

1.找出所有子集的异或总和在求和

找出所有子集的异或总和在求和
在这里插入图片描述

class Solution {
    int ret =0;
    int path =0;
public:
    int subsetXORSum(vector<int>& nums) {
        dfs(nums,0);
        return ret;
    }
    void dfs(vector<int>& nums,int pos)
    {
        ret += path;
        for(int i=pos;i<nums.size();i++)
        {
            path ^= nums[i];
            dfs(nums,i+1);
            path ^= nums[i];//异或的消消乐原理
        }
    }
};

2.全排列II

全排列II
在这里插入图片描述

class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    bool check[9];
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        dfs(nums,0);
        return ret;
    }
    // //法一:只关心不合法的,也就是不满足全排列要求的都剪枝掉
    // void dfs(vector<int>& nums,int pos)
    // {
    //     if(pos == nums.size())
    //     {
    //         ret.push_back(path);
    //         return;
    //     }
    //     for(int i=0;i<nums.size();i++)
    //     {
    //         if(check[i] == true||(i!=0&&nums[i] == nums[i-1]&&check[i-1] == false))
    //         {
    //             continue;
    //         }
    //         path.push_back(nums[i]);
    //         check[i] = true;
    //         dfs(nums,pos+1);
    //         path.pop_back();
    //         check[i] = false;
    //     }
    // }
    //法二:只关心合法的,也就是满足全排列要求的
    void dfs(vector<int>& nums,int pos)
    {
        if(pos == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for(int i=0;i<nums.size();i++)
        {
            if(check[i] == false&&(i==0||nums[i]!=nums[i-1]||check[i-1] == true))
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums,pos+1);
                path.pop_back();
                check[i] = false;
            }
        }
    }
};

3.电话号码的字母组合

电话号码的字母组合
在这里插入图片描述

class Solution {
    string path;
    vector<string> ret;
    string hash[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public:
    vector<string> letterCombinations(string digits) {
        if(digits.size() == 0)
        {
            return ret;
        }
        dfs(digits,0);
        return ret;
    }
    void dfs(string& digits, int pos)
    {
        if(pos == digits.size())
        {
            ret.push_back(path);
            return;
        }
        for(auto ch:hash[digits[pos]-'0'])
        {
            path.push_back(ch);
            dfs(digits,pos+1);
            path.pop_back();
        }
    }
};

4.括号生成

括号生成
在这里插入图片描述

class Solution {
    vector<string> ret;
    string path;
    int left,right,n;
public:
    //策略:左括号的数量等于右括号的数量
    //从头开始,左括号的数量大于等于右括号的数量
    vector<string> generateParenthesis(int _n) {
        n = _n;
        dfs();
        return ret;
    }
    void dfs()
    {
        if(right == n)
        {
            ret.push_back(path);
            return;
        }
        if(left<n)
        {
            path.push_back('(');
            left++;
            dfs();
            path.pop_back();
            left--;
        }
        if(left>right)
        {
            path.push_back(')');
            right++;
            dfs();
            path.pop_back();
            right--;
        }
    }
};

5.组合

组合
在这里插入图片描述

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);
        }
        for(int i=pos;i<=n;i++)
        {
            path.push_back(i);
            dfs(i+1);
            path.pop_back();
        }
    }
};

6.目标和

目标和
在这里插入图片描述

class Solution {
    int ret;
    int aim;
    int n;
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        aim = target;
        n = nums.size();
        dfs(nums,0,0);
        return ret;
    }
    void dfs(vector<int>& nums,int pos,int path)
    {
        //path做参数
        if(pos == nums.size())
        {
            if(path == aim) 
            {
                ret++;
            }
            return;
        }
        dfs(nums,pos+1,path+nums[pos]);
        dfs(nums,pos+1,path-nums[pos]);
    }
};
class Solution {
    int ret;
    int aim;
    int path;
    int n;
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        aim = target;
        n = nums.size();
        dfs(nums,0,0);
        return ret;
    }
    void dfs(vector<int>& nums,int pos,int path)
    {
        //path做全局变量
        if(pos == nums.size())
        {
            if(path == aim) 
            {
                ret++;
            }
            return;
        }
        path +=nums[pos];
        dfs(nums,pos+1,path);
        path -= nums[pos];

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

7.组合总和

组合总和
在这里插入图片描述

class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    int aim;
public:
    vector<vector<int>> combinationSum(vector<int>& c, int target) {
        aim = target;
        dfs(c,0,0);
        return ret;
    }
    void dfs(vector<int>& c,int pos,int sum)
    {
        if(sum>aim) return;
        if(sum == aim)
        {
            ret.push_back(path);
            return;
        }
        for(int i=pos;i<c.size();i++)
        {
            path.push_back(c[i]);
            dfs(c,i,sum+c[i]);
            path.pop_back();
        }
    }
};

8.字母大小写全排列

字母大小写全排列
在这里插入图片描述

class Solution {
    vector<string> ret;
    string path;
public:
    vector<string> letterCasePermutation(string s) {
        dfs(s,0);
        return ret;
    }

    void dfs(string& s,int pos)
    {
        if(pos == s.size())
        {
            ret.push_back(path);
            return;
        }

        if(s[pos]<'0' || s[pos]>'9')
        {
            //变
            path.push_back(change(s[pos]));
            dfs(s,pos+1);
            path.pop_back();
        }
        //不变
        path.push_back(s[pos]);
        dfs(s,pos+1);
        path.pop_back();
    }
    char change(char ch)
    {
        if(ch>='a'&&ch<='z') ch -= 32;
        else ch += 32;
        return ch;
    }

};

9.优美的排列

优美的排列
在这里插入图片描述

class Solution {
    bool check[16];
    int ret;
    int n;
public:
    int countArrangement(int _n) {
        n = _n;
        dfs(1);
        return ret;
    }

    void dfs(int pos)
    {
        if(pos == n+1)
        {
            ret++;
            return;
        }
        for(int i=1;i<=n;i++)
        {
            if(!check[i] && (i%pos ==0 || pos%i == 0))
            {
                check[i] = true;
                dfs(pos+1);
                check[i] = false;
            }
        }
    }
};

http://www.niftyadmin.cn/n/5309305.html

相关文章

Kali Linux——获取root权限

目录 一、设置root密码 【操作命令】 【操作实例】 二、临时获取root权限 【操作命令】 【操作实例】 三、提升用户到root 1、获取root权限 2、进入/etc/passwd 3、查看root账号ID 4、找到需要修改的用户 5、输入i&#xff0c;进入编辑模式 6、把用户的ID改成跟r…

生成式AI在自动化新时代中重塑RPA

生成式AI的兴起正在推动行业的深刻变革&#xff0c;其与RPA技术的结合&#xff0c;标志着自动化领域新时代的到来。这种创新性结合极大地提升了系统的适应性&#xff0c;同时也推动了高级自动化解决方案的发展&#xff0c;为下一代RPA的诞生奠定了坚实的基础。 核心RPA技术专注…

阿里云公网带宽出网和入网是什么?上行和下行是什么?

什么是阿里云服务器ECS的入网带宽和出网带宽&#xff1f;以云服务器为中心&#xff0c;流入云服务器占用的带宽是入网带宽&#xff0c;流量从云服务器流出的带宽是出网带宽。阿里云服务器网aliyunfuwuqi.com分享入网带宽和出网带宽说明表&#xff1a; 带宽类别说明入网带宽&am…

CSS 压重按钮 效果

<template><view class="cont"><div class="container"><div class="pane"><!-- 选项1 --><label class="label" @click="handleOptionClick(0)":style="{ color: selectedOption ==…

MySQL——用户管理

目录 一.用户管理 二.用户 1.用户信息 2.创建用户 3.删除用户 4. 修改用户密码 三.数据库的权限 1.给用户授权 2.回收权限 一.用户管理 如果我们只能使用root用户&#xff0c;root的权限非常大,这样存在安全隐患。这时&#xff0c;就需要使用MySQL的用户管理&#xff…

最新Tomcat下载安装详细教程

Tomcat下载安装教程 Tomcat简介Tomcat下载tomcat安装验证安装是否成功 Tomcat简介 Tomcat是什么&#xff1f; Tomcat是web容器。你在做web项目时&#xff0c;多数需要http协议&#xff0c;也就是基于请求和响应&#xff0c;比如你在百度输入一行内容搜索&#xff0c;那么百度服…

Qt3D 纹理模块使用说明

在 Qt3D 中&#xff0c;纹理相关的类用于定义和管理3D对象的纹理贴图&#xff0c;以及与纹理相关的属性和特性。 1. **Qt3DRender::QAbstractTexture** - QAbstractTexture 类是纹理的抽象基类&#xff0c;用于表示2D、3D或立方体纹理。 - 使用 QAbstractTexture 可以创…

概率论与数理统计 知识点+课后习题

文章目录 &#x1f496; [学习资源整合](https://www.cnblogs.com/duisheng/p/17872980.html)&#x1f4da; 总复习&#x1f4d5; 知识点⭐ 常用分布的数学期望和方差 &#x1f4d9; 选择题&#x1f4d9; 填空题&#x1f4d9; 大题1. 概率2. 概率3. 概率4. P5. 概率6. 概率密度…