当前位置: 首页 > news >正文

力扣 hot100 Day58

完成了这些题目,都是回溯题目,思路比较统一,懒得一一贴代码了,反正是记录向而非教学向

class Solution {
private:vector<vector<int>>result;vector<int> path;public:void backtracking(vector<int>& nums,int index){result.push_back(path);for(int i=index;i<nums.size();i++){path.push_back(nums[i]);backtracking(nums,i+1);path.pop_back();}}vector<vector<int>> subsets(vector<int>& nums) {int index = 0;backtracking(nums,index);return result;}
};

这是子集的求解代码,用于返回给定数组所有可能的子集

大致思路就是,在每一层backtracking函数中,再用for循环递归多个backtracking,这些层其实就类似于实现多层for循环遍历了。

这里的子集需要考虑顺序,所以加入参数index,在for循环中应用

class Solution {
private:vector<string> res;string path;
public:void backtracking(int n,int left,int right){if(left==right&&left==n){res.push_back(path);return;}if(left<n){path.push_back('(');backtracking(n,left+1,right);path.pop_back();}if(right<left){path.push_back(')');backtracking(n,left,right+1);path.pop_back();}}vector<string> generateParenthesis(int n) {backtracking(n,0,0);return res;}
};

这个代码用于返回n对括号所有可能组合

不同于for循环,这里只是简单进行条件判断,当左括号数小于n时,可以加入左括号,当右括号数小于左括号数时,可以加入右括号

class Solution {
private:vector<vector<string>> result;void backtracking(int n, int row, vector<string>& chessboard) {if (row == n) {result.push_back(chessboard);return;}for (int col = 0; col < n; col++) {if (isValid(row, col, chessboard, n)) {chessboard[row][col] = 'Q';backtracking(n, row + 1, chessboard);chessboard[row][col] = '.';}}}bool isValid(int row, int col, vector<string>& chessboard, int n) {for (int i = 0; i < row; i++) {if (chessboard[i][col] == 'Q') {return false;}}for (int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--) {if (chessboard[i][j] == 'Q') {return false;}}for(int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {if (chessboard[i][j] == 'Q') {return false;}}return true;}
public:vector<vector<string>> solveNQueens(int n) {result.clear();vector<string> chessboard(n, string(n, '.'));backtracking(n, 0, chessboard);return result;}
};

这是n皇后的代码,返回n*n的矩阵中不能同行同列同斜线的皇后数

这里将棋盘的宽度设为for循环的长度,递归的深度就是棋盘的高度,相当于从第一行开始,一行放一个皇后,遍历所有可能,进行递归判断

其它都大差不差,可以多回顾看看

http://www.lryc.cn/news/602399.html

相关文章:

  • eclipse更改jdk环境和生成webservice客户端代码
  • STM32入门之DMA直接存储器存取
  • 雷达系统设计学习:自制6GHz FMCW Radar
  • 从单枪匹马到联盟共生:白钰玮的 IP 破局之路|创客匠人
  • 【智慧物联网平台】编译jar环境 Linux 系统Maven 安装——仙盟创梦IDE
  • 2025创始人IP如何破局?
  • 【智慧物联网平台】编译jar环境 Linux 系统编译IOT物联网——仙盟创梦IDE
  • 解构远程智能系统的视频能力链:从RTSP|RTMP协议接入到Unity3D头显呈现全流程指南
  • Ansible安装与入门
  • WPF,按钮透明背景实现MouseEnter
  • 【Linux】Ubuntu上安装.NET 9运行时与ASP.NET Core项目部署入门
  • C#/.NET/.NET Core技术前沿周刊 | 第 48 期(2025年7.21-7.27)
  • 1.gradle安装(mac)
  • 基于AFLFast的fuzz自动化漏洞挖掘(1)
  • 全新AI工具小程序源码 全开源
  • 时序数据库选型指南:工业大数据场景下基于Apache IoTDB技术价值与实践路径
  • Verilog简易的按键消抖模块
  • css 实现虚线效果的多种方式
  • Kubernetes 存储入门
  • 【自动化运维神器Ansible】Ansible常用模块之unarchive模块详解
  • 快速入门Linux操作系统(二)
  • 腾讯AI IDE
  • 《红色脉络:一部PLMN在中国的演进史诗 (1G-6G)》 第3篇 | 2G:GSM一统江湖?——移动、联通的“分家”与双轨并行
  • windows平台计划任务批处理实现定时任务
  • 零基础学习性能测试第九章:全链路追踪-系统中间件节点监控
  • DDD领域驱动中瘦模型与富态模型的核心区别
  • FastGPT本地构建工作流高级编排(最新4.11.0)
  • 火狐浏览器中国特供版关闭,如何下载 Firefox 国际版?如何备份数据?
  • App Inventor 2 使用 MaterialIcons 图标字体,快捷展示专业图标
  • NAS远程访问新解法:OMV与cpolar的技术协同价值