dfs+回溯写题两种思路

2023-10-10 10:08
文章标签 两种 dfs 思路 回溯 写题

本文主要是介绍dfs+回溯写题两种思路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

dfs+回溯写题两种思路

主要框架

	public void dfs(选择列表){//1.找到结束条件//2.遍历所有可能性//2.1做选择//2.2 递归调用自己进一步深度遍历//3.回撤选择
}
  • dfs函数的参数变量我觉得是越少越好,所以将一些不怎么改变的变量设置为全局变量更容易理清思路

1.遍历的过程不停的往中间变量添加数据

剑指 Offer 38. 字符串的排列

	static Set<String> res;static char[] ch;static boolean[] ch_helper;public static String[] permutation(String s) {res = new HashSet<>();ch = s.toCharArray();ch_helper = new boolean[ch.length];dfs("");return res.toArray(new String[res.size()]);}private static void dfs(String str) {//1.结束条件if(str.length() == ch.length){res.add(str);}//2.遍历所有可能性for(int i = 0; i < ch.length; i++){if(ch_helper[i] != true){ch_helper[i] = true;dfs(str + ch[i]);ch_helper[i] = false;}}}

46. 全排列

	static List<List<Integer>> res;static LinkedList<Integer> tmp;public static List<List<Integer>> permute(int[] nums) {res = new LinkedList<>();tmp = new LinkedList<>();dfs(nums);return res;}private static void dfs(int[] nums) {if(tmp.size() == nums.length){res.add(new ArrayList<>(tmp));return;}for(int num : nums){if(tmp.contains(num)){continue;}tmp.add(num);dfs(nums);tmp.removeLast();}

2. 遍历的过程改变原列表,这种一般会从下标0开始

剑指 Offer 38. 字符串的排列

static List<String> res;static char[] ch;public static String[] permutation(String s) {res = new LinkedList<>();ch = s.toCharArray();dfs(0);return res.toArray(new String[res.size()]);}private static void dfs(int i) {//1.结束条件if (i == ch.length - 1){res.add(String.valueOf(ch));return;}//2.选择列表Set<Character> set = new HashSet<>();for(int idx = i; idx < ch.length; idx++){if(set.contains(ch[idx]))continue;set.add(ch[idx]);//2.1选择swap(ch, idx, i);//2.2 进一步dfs(i + 1);//3.回溯swap(ch, idx, i);}}private static void swap(char[] ch, int idx, int i) {char tmp = ch[idx];ch[idx] = ch[i];ch[i] = tmp;}

46. 全排列

  1. 么有剪枝
static List<String> res;static char[] ch;public static String[] permutation(String s) {res = new LinkedList<>();ch = s.toCharArray();dfs(0);return res.toArray(new String[res.size()]);}private static void dfs(int i) {//1.结束条件if (i == ch.length - 1){res.add(String.valueOf(ch));return;}//2.选择列表for(int idx = i; idx < ch.length; idx++){set.add(ch[idx]);//2.1选择swap(ch, idx, i);//2.2 进一步dfs(i + 1);//3.回溯swap(ch, idx, i);}}private static void swap(char[] ch, int idx, int i) {char tmp = ch[idx];ch[idx] = ch[i];ch[i] = tmp;}
  1. 带剪枝
	static List<String> res;static char[] ch;public static String[] permutation(String s) {res = new LinkedList<>();ch = s.toCharArray();dfs(0);return res.toArray(new String[res.size()]);}private static void dfs(int i) {//1.结束条件if (i == ch.length - 1){res.add(String.valueOf(ch));return;}//2.选择列表Set<Character> set = new HashSet<>();for(int idx = i; idx < ch.length; idx++){if(set.contains(ch[idx]))continue;set.add(ch[idx]);//2.1选择swap(ch, idx, i);//2.2 进一步dfs(i + 1);//3.回溯swap(ch, idx, i);}}private static void swap(char[] ch, int idx, int i) {char tmp = ch[idx];ch[idx] = ch[i];ch[i] = tmp;}

这篇关于dfs+回溯写题两种思路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/179758

相关文章

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

CentOS7增加Swap空间的两种方法

《CentOS7增加Swap空间的两种方法》当服务器物理内存不足时,增加Swap空间可以作为虚拟内存使用,帮助系统处理内存压力,本文给大家介绍了CentOS7增加Swap空间的两种方法:创建新的Swa... 目录在Centos 7上增加Swap空间的方法方法一:创建新的Swap文件(推荐)方法二:调整Sww

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

Python MCPInspector调试思路详解

《PythonMCPInspector调试思路详解》:本文主要介绍PythonMCPInspector调试思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录python-MCPInspector调试1-核心知识点2-思路整理1-核心思路2-核心代码3-参考网址

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

Windows 上如果忘记了 MySQL 密码 重置密码的两种方法

《Windows上如果忘记了MySQL密码重置密码的两种方法》:本文主要介绍Windows上如果忘记了MySQL密码重置密码的两种方法,本文通过两种方法结合实例代码给大家介绍的非常详细,感... 目录方法 1:以跳过权限验证模式启动 mysql 并重置密码方法 2:使用 my.ini 文件的临时配置在 Wi

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

Win11安装PostgreSQL数据库的两种方式详细步骤

《Win11安装PostgreSQL数据库的两种方式详细步骤》PostgreSQL是备受业界青睐的关系型数据库,尤其是在地理空间和移动领域,:本文主要介绍Win11安装PostgreSQL数据库的... 目录一、exe文件安装 (推荐)下载安装包1. 选择操作系统2. 跳转到EDB(PostgreSQL 的