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

力扣-图论-2【算法学习day.52】

前言

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


习题

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);}
}

后言

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

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

相关文章:

  • MySQL如何区分幻读和不可重复读
  • 界面控件Syncfusion Essential Studio®现在已完全支持 .NET 9
  • openEuler安装lsb_release
  • 统计数字字符个数
  • 44页PDF | 信息化战略规划标准框架方法论与实施方法(限免下载)
  • 计算机网络期末复习-part1-概述
  • A1228 php+Mysql旅游供需平台的设计与实现 导游接单 旅游订单 旅游分享网站 thinkphp框架 源码 配置 文档 全套资料
  • RabbitMQ消息可靠性保证机制5--消息幂等性处理
  • Claude3.5如何使用?
  • 力扣刷题TOP101:14.BM16 删除有序链表中重复的元素-II
  • 解决github网络慢的问题
  • docker及docker exec命令学习笔记
  • linux环境宝塔服务部署安装及介绍
  • 充分统计量(Sufficient Statistic)概念与应用: 中英双语
  • 基于Matlab计算机视觉的车道线识别与前车检测系统研究
  • 模糊测试中常见的10种变异mutation策略
  • opencv-android编译遇到的相关问题处理
  • 把 py脚本生成windows 可执行的文件
  • 云计算的发展历史与未来展望
  • 基于飞腾S2500处理器的全国产加固服务器
  • gitlab-cicd部署安装与具体操作
  • 2022高等代数上【南昌大学】
  • 文本生成类(机器翻译)系统评估
  • 11.7【miniob】【debug】
  • OSHI 介绍与使用
  • Hadoop生态圈框架部署(八)- Hadoop高可用(HA)集群部署
  • 【RocketMQ】Name Server 无状态特点及如何让 Broker Consumer Producer 感知新节点
  • 蓝牙定位的MATLAB程序,四个锚点、三维空间
  • 机器学习--绪论
  • Unity 设计模式-命令模式(Command Pattern)详解