当前位置: 首页> 房产> 市场 > 莱芜网页制作公司_无极在线招聘招工最新招聘_百度指数人群画像哪里查询_企业宣传片文案

莱芜网页制作公司_无极在线招聘招工最新招聘_百度指数人群画像哪里查询_企业宣传片文案

时间:2025/7/13 2:22:50来源:https://blog.csdn.net/2301_79232523/article/details/144250844 浏览次数:2次
莱芜网页制作公司_无极在线招聘招工最新招聘_百度指数人群画像哪里查询_企业宣传片文案

前言

###我做这类文章一个重要的目的还是给正在学习的大家提供方向和记录学习过程(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!


习题

1.所有可能的路径

题目链接:797. 所有可能的路径 - 力扣(LeetCode)

题面:

分析:简单的dfs

代码:

class Solution {List<List<Integer>> ans = new ArrayList<>();int n;int[][] graph;public List<List<Integer>> allPathsSourceTarget(int[][] graph) {List<Integer> list  = new ArrayList<>();n = graph.length;this.graph = graph;int[] flag = new int[n];list.add(0);recursion(list,0,flag);return ans;}public void recursion(List<Integer> list,int x,int[] flag){//  System.out.println(x);if(x==(n-1)){// System.out.println(1);ans.add(new ArrayList<>(list));}int[] arr = graph[x];int m = arr.length;for(int i = 0;i<m;i++){if(flag[arr[i]]==0){list.add(arr[i]);flag[arr[i]] = 1;recursion(list,arr[i],flag);flag[arr[i]] = 0;list.remove(list.size()-1);}} }
}

2.钥匙和房间

题目链接:841. 钥匙和房间 - 力扣(LeetCode)

 题面:

代码:

class Solution {int n;int[] have;int count;int[] flag;List<List<Integer>> rooms;public boolean canVisitAllRooms(List<List<Integer>> rooms) {this.rooms = rooms;n = rooms.size();List<Integer> list = rooms.get(0);have = new int[n];flag = new int[n];flag[0] = 1;count = n-1;have[0] = 1;for(int a:list){if(have[a]==0)count--;have[a] = 1;}recursion(have);System.out.println(count);return count==0?true:false;}public void recursion(int[] have){int blog = 0;for(int i = 0;i<n;i++){if(have[i]==1&&flag[i]==0){flag[i] = 1;List<Integer> list = rooms.get(i);for(int a:list){if(have[a]==0)count--;have[a] = 1;}blog = 1;}}if(blog==1)recursion(have);}
}

后言

上面是力扣图论专题,下一篇是其他的习题,希望有所帮助,一同进步,共勉!

关键字:莱芜网页制作公司_无极在线招聘招工最新招聘_百度指数人群画像哪里查询_企业宣传片文案

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: