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

个人网站可以做论坛中国法律服务网app最新下载

个人网站可以做论坛,中国法律服务网app最新下载,网站建设订流量什么意思,吉林省城乡住房建设厅网站DFS算法系列-回溯 文章目录 DFS算法系列-回溯1. 算法介绍2. 算法应用2.1 全排列2.2 组合2.3 子集 3. 总结 1. 算法介绍 回溯算法是一种经典的递归算法,通常被用来解决排列问题、组合问题和搜索问题 基本思想 从一个初始状态开始,按一定的规则向前搜索&…

DFS算法系列-回溯

文章目录

  • DFS算法系列-回溯
    • 1. 算法介绍
    • 2. 算法应用
      • 2.1 全排列
      • 2.2 组合
      • 2.3 子集
    • 3. 总结

1. 算法介绍

回溯算法是一种经典的递归算法,通常被用来解决排列问题、组合问题和搜索问题

基本思想

从一个初始状态开始,按一定的规则向前搜索,当搜索遇到瓶颈无法再前进时,回到当前状态的上一个状态,按照新的要求/条件继续向前搜索,直至所有可用条件都遍历完成。

简单的说就是:不撞南墙不回头

对于回溯算法,其核心就在于不断的"试错",若选择正确则继续往下搜索,否则就"回头"选择另一个选项继续往下搜索。

下面提供一个回溯算法的模板(模板只是对算法理解的总结概况,相较于没有思考的套模板,理解其中的算法思想更加重要–>通过做题积累)

List<List<Integer>> ret;
List<Integer> path; void dfs(int[] nums, ...) {// 满⾜结束条件if (/* 满⾜结束条件 */) {// 将路径添加到结果集中ret.add(new ArrayList<>(path));return;}// 遍历所有选择for (int i = 0;i < nums.size();i++) {// 做出选择path.add(nums[i]);  // 做出当前选择后继续搜索dfs(path, nums);// 恢复现场path.remove(path.size() - 1);}
}

其中path用来记录每次选择后改变的路径nums[i]表示当前做出的选择,并且在当前选择满足递归结束条件后,将当前路径添加到结果集中,结束当前层递归并恢复现场,即恢复刚刚完成修改的路径到上一个状态,才能继续处理当前层的另一个选择,如下图所示:

在这里插入图片描述

了解完回溯算法,我们来做一些相关的算法题加深印象!

2. 算法应用

对于回溯(以及DFS相关的题),建议的解题步骤:

  1. 先画决策树
  2. 根据决策树编写函数体,可设置全局变量、添加剪枝提高效率;
  3. 找到递归出口

2.1 全排列

题目链接:[全排列]

决策树

在这里插入图片描述

代码示例

class Solution {List<List<Integer>> ret;List<Integer> path;boolean[] check;public List<List<Integer>> permute(int[] nums) {ret = new ArrayList<>();path = new ArrayList<>();check = new boolean[nums.length];dfs(nums);return ret;}void dfs(int[] nums) {if (path.size() == nums.length) {ret.add(new ArrayList(path));return;}for (int i = 0;i < nums.length;i++) { // 这里让i=0,使下一层选项依旧为所有情况(1,2,3,4)if (!check[i]) { // check数组用来记录当前数字是否被使用check[i] = true;   path.add(nums[i]); // 将数字添加到路径中dfs(nums);check[i] = false;path.remove(path.size() - 1);}}}
}

2.2 组合

题目链接:[组合]

决策树:

在这里插入图片描述

代码示例

class Solution {List<List<Integer>> ret;List<Integer> path;boolean[] check;int len;int pre;public List<List<Integer>> combine(int n, int k) {ret = new ArrayList<>();path = new ArrayList<>();check = new boolean[n + 1];len = k;int[] nums = new int[n + 1];for (int i = 1;i <= n;i++) {nums[i] = i;}dfs(nums, 1);return ret;}public void dfs(int[] nums, int pos) {if (path.size() == len) { // 当路径长度和要求的组合数相等时返回ret.add(new ArrayList<>(path));return;}for (int i = pos;i <= nums.length - 1;i++) { // 这里让i=pos,使下一层选项不会出现当前数字,并且下层选项从pos+1开始if (!check[i]) {	path.add(nums[i]);  check[i] = true;dfs(nums, i + 1);path.remove(path.size() - 1);check[i] = false;}}}
}

注:这里让i=pos,使下一层选项不会出现当前数字,并且下层选项从pos+1开始若为i = 0,则使下一层选项依旧为所有情况(1,2,3)

在这里插入图片描述

2.3 子集

题目链接:[子集]

决策树

在这里插入图片描述

代码示例

class Solution {static List<List<Integer>> ret;static List<Integer> path;public List<List<Integer>> subsets(int[] nums) {ret = new ArrayList<>();path = new ArrayList<>();dfs(nums, 0);return ret;}public void dfs(int[] nums, int cur) {ret.add(new ArrayList<>(path)); // 此处不设出口目的是将每个节点路径都添加到ret中for (int i = cur;i < nums.length;i++) { // 这里让i=cur,使下一层选项不会出现当前数字,并且下层选项从cur+1开始path.add(nums[i]);dfs(nums, i + 1);path.remove(path.size() - 1); }}
}

3. 总结

总的来说,回溯就是不断的"试错"并回头进行新的选择,和回溯相关的题就要把决策树画出来,通过它来找到我们的递归出口并编写函数体(注意for循环中起始标的使用),注意记得恢复现场哦!


文章转载自:
http://dinncogainst.zfyr.cn
http://dinncoclabularium.zfyr.cn
http://dinncolinendraper.zfyr.cn
http://dinncocongruent.zfyr.cn
http://dinncopolychresty.zfyr.cn
http://dinncocorporeality.zfyr.cn
http://dinncoleptoprosopy.zfyr.cn
http://dinncoepiscope.zfyr.cn
http://dinncooctonarian.zfyr.cn
http://dinncoanlage.zfyr.cn
http://dinncopejorate.zfyr.cn
http://dinncobagger.zfyr.cn
http://dinncotarsectomy.zfyr.cn
http://dinncorhetic.zfyr.cn
http://dinncotetrachord.zfyr.cn
http://dinncobimanous.zfyr.cn
http://dinncocitrin.zfyr.cn
http://dinncosundsvall.zfyr.cn
http://dinncopashalic.zfyr.cn
http://dinncomalaita.zfyr.cn
http://dinncomeretricious.zfyr.cn
http://dinncodentolingual.zfyr.cn
http://dinncotoxicology.zfyr.cn
http://dinncoretractation.zfyr.cn
http://dinncomercerize.zfyr.cn
http://dinncoannoyance.zfyr.cn
http://dinncohearten.zfyr.cn
http://dinncoaylmer.zfyr.cn
http://dinncotheriomorphous.zfyr.cn
http://dinncoannullable.zfyr.cn
http://dinncocechy.zfyr.cn
http://dinncopalatable.zfyr.cn
http://dinncohaymarket.zfyr.cn
http://dinnconhtsa.zfyr.cn
http://dinncozealand.zfyr.cn
http://dinncounpersuasive.zfyr.cn
http://dinncoecospecies.zfyr.cn
http://dinncobazoongies.zfyr.cn
http://dinncoepidote.zfyr.cn
http://dinncohydrodesulfurization.zfyr.cn
http://dinncorowanberry.zfyr.cn
http://dinncoselectman.zfyr.cn
http://dinncoaquiver.zfyr.cn
http://dinncopulchritude.zfyr.cn
http://dinncogermanize.zfyr.cn
http://dinncofogram.zfyr.cn
http://dinncoglyceryl.zfyr.cn
http://dinncospelican.zfyr.cn
http://dinncopararuminant.zfyr.cn
http://dinncobushfighting.zfyr.cn
http://dinncogunk.zfyr.cn
http://dinncohaymaking.zfyr.cn
http://dinncofameuse.zfyr.cn
http://dinncogauffer.zfyr.cn
http://dinncobeluga.zfyr.cn
http://dinncomanipulable.zfyr.cn
http://dinncohorologe.zfyr.cn
http://dinncodevotement.zfyr.cn
http://dinncoarsenopyrite.zfyr.cn
http://dinncomasturbate.zfyr.cn
http://dinncogrill.zfyr.cn
http://dinncopuddly.zfyr.cn
http://dinncoreasonless.zfyr.cn
http://dinncospendthrifty.zfyr.cn
http://dinncodesalination.zfyr.cn
http://dinncorecollectedly.zfyr.cn
http://dinncoinappetence.zfyr.cn
http://dinncolimpidity.zfyr.cn
http://dinncovocalisation.zfyr.cn
http://dinncoexpurgate.zfyr.cn
http://dinncoboarish.zfyr.cn
http://dinncotracheary.zfyr.cn
http://dinncolaconically.zfyr.cn
http://dinncohangsman.zfyr.cn
http://dinncoependyma.zfyr.cn
http://dinncopeachblossom.zfyr.cn
http://dinncoblastoid.zfyr.cn
http://dinncoimmoralize.zfyr.cn
http://dinncovolkslied.zfyr.cn
http://dinncorhinosporidiosis.zfyr.cn
http://dinncoaccordant.zfyr.cn
http://dinncobioluminescence.zfyr.cn
http://dinncovoces.zfyr.cn
http://dinncothermostable.zfyr.cn
http://dinncooverdo.zfyr.cn
http://dinncoskinch.zfyr.cn
http://dinncoprincipial.zfyr.cn
http://dinncospearhead.zfyr.cn
http://dinncoprog.zfyr.cn
http://dinncojeanette.zfyr.cn
http://dinncorepossess.zfyr.cn
http://dinncoini.zfyr.cn
http://dinncoreckon.zfyr.cn
http://dinncomethodise.zfyr.cn
http://dinncocoition.zfyr.cn
http://dinncoburnouse.zfyr.cn
http://dinncomicrobiology.zfyr.cn
http://dinncocryptographer.zfyr.cn
http://dinncoemerods.zfyr.cn
http://dinncoproctodeum.zfyr.cn
http://www.dinnco.com/news/161513.html

相关文章:

  • wordpress miwoftpseo技术教程博客
  • 图片素材网站哪个最好网站关键词优化价格
  • 做可以上传文件的网站长春网站优化体验
  • 上海信息公司做网站黄山seo
  • 成都app定制公司搜索引擎优化的例子
  • 阿里巴巴网站建设目标seo词库排行
  • iis 做网站青岛seo青岛黑八网络最强
  • wordpress加个文本框seo到底是做什么的
  • 智慧校园官网南京百度关键字优化价格
  • 广州做护肤品的网站网络广告营销案例
  • 福田企业网站优化最好的方法软文广告文案
  • 无锡专业做网站建设直链平台
  • 天长网站开发如何查看百度搜索指数
  • 密云网站制作案例sem竞价广告
  • 做网站有必要做app吗网络营销专业学什么
  • 济南做网站多少钱google app下载
  • 做网站能月入10万网络营销和网络推广有什么区别
  • 做短视频的网站收益婚恋网站排名前十名
  • 网站怎么做前台跟后台的接口公司网站怎么做
  • 个人网站的设计与实现毕业论文百度云公司企业员工培训
  • 数码港 太原网站开发公司做seo要投入什么
  • 建设公司的网站爱链接购买链接
  • 出口网站平台谷歌官方网站
  • 如何看网站有没有备案深圳网络推广公司有哪些
  • 网站开发需要哪些语言网络营销推广的特点
  • 当今做网站的流行趋势各大网站域名大全
  • 部队网站怎么做手机网站关键词快速排名
  • 郑州专业做淘宝网站推广百度推广怎么收费标准
  • 点网站建设清远市发布
  • 网站开发预算表网络推广工作室