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

图论① dfs | Java | LeetCode 797,Kama 98 邻接表实现(未完成)

797 所有可能路径

https://leetcode.cn/problems/all-paths-from-source-to-target/description/
输入:graph = [[1,2],[3],[3],[]]

题目分析,这里

class Solution {//这个不是二维数组,而是listList<List<Integer>> res = new ArrayList<List<Integer>>();List<Integer> path = new ArrayList<>();public List<List<Integer>> allPathsSourceTarget(int[][] graph) {path.add(0);dfs(graph, 0);return res;}public void dfs(int graph[][], int x) {if(x == graph.length-1) { //横向的所有节点res.add(new ArrayList<>(path));return;}//列for(int i=0; i<graph[x].length; i++) {path.add(graph[x][i]);dfs(graph, graph[x][i]);path.remove(path.size()-1);}}
}

Kama 98

题目链接 https://kamacoder.com/problempage.php?pid=1170

邻接矩阵实现

import java.util.*;public class Main {static List<List<Integer>> res = new ArrayList<List<Integer>>();static List<Integer> path = new ArrayList<Integer>();public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int[][]graph = new int[N+1][N+1];//这个for循环for(int i=0; i<M; i++) {int s = sc.nextInt();int t = sc.nextInt();graph[s][t] = 1;}path.add(1);dfs(graph, 1);if (res.isEmpty()) System.out.println(-1);for (List<Integer> pa : res) {for (int i = 0; i < pa.size() - 1; i++) {System.out.print(pa.get(i) + " ");}System.out.println(pa.get(pa.size() - 1));}}public static void dfs(int[][]graph, int x) {//节点个数int n = graph.length-1;if(x == n) {res.add(new ArrayList<>(path));return;}for(int i=1; i<=n; i++) {if(graph[x][i] == 1) {path.add(i);dfs(graph ,i);path.remove(path.size()-1);}}}
}

出错点

1 与力扣上的题目不一样,首先需要自己设置输入输出

输入的时候,这里for循环是因为告知了有多少条有向边

for(int i=0; i<M; i++) {int s = sc.nextInt();int t = sc.nextInt();graph[s][t] = 1;
}

2 力扣直接给出了图 int graph[][],而且输入样例中 输入:graph = [[1,2],[3],[3],[]],直接表明这个二维数组不是 n×n 的,因此graph.length获得节点的个数,graph[index].length获得以节点index为起点 的边的总数,

  • 力扣797
    在这里插入图片描述

  • 对比:在卡玛98题中,我们在用邻接矩阵创建图的时候,graph是n×n的:int[][]graph = new int[N+1][N+1];。因此在回溯(DFS)的时候,for循环条件不一样,也要多一个if判断
    在这里插入图片描述

3 static的使用,在Kama的ACM模式下 public static void main(String[] args)是入口函数,直接运行了,因此这里定义的函数和变量都需要用 static描述,否则必须要先实例化类Main才能调用函数
在这里插入图片描述

4 Kama的输出:遍历 List<List<Integer>> res

if (res.isEmpty()) System.out.println(-1);for (List<Integer> pa : res) {for (int i = 0; i < pa.size() - 1; i++) {System.out.print(pa.get(i) + " ");}System.out.println(pa.get(pa.size() - 1));}

邻接表实现(未完成)

LinkedList

链表是一种常见的数据结构,用于存储和组织数据。它由一系列节点(Node)组成,每个节点包含两个主要部分:数据域(Data)和指针域(Pointer)。

数据域存储节点所需的数据或信息,可以是任意类型的数据,如整数、字符、对象等。指针域则指向链表中的下一个节点,将节点连接起来形成链表结构。

链表中的节点并不一定按照物理上的连续位置存储,而是通过指针域相互连接。这使得链表能够灵活地插入、删除和修改节点,而无需像数组那样进行元素的移动。

链表的优点是可以高效地插入和删除节点,而无需移动其他节点。然而,由于链表中的节点不是连续存储的,访问特定位置的节点需要从头部开始遍历,因此随机访问的效率较低。

在Java中,LinkedList(链表)是Java集合框架提供的一个实现了List接口的类。它是基于双向链表结构实现的,可以用于存储和操作元素的有序集合。

【Java】链表LinkedList

LinkedList和ArrayList是Java集合框架中的两种不同的List实现。

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

相关文章:

  • Mac安装nvm以及配置环境变量
  • AUTOSAR实战教程-使用DET来发现开发错误
  • ZeroMQ(二):请求-响应模式,C和C++。
  • 【虚拟仿真】Unity3D中实现2DUI显示在3D物体旁边
  • 代码随想录 day 29 贪心
  • 开源:LLMCompiler高性能工具调用框架
  • 【学习方法】高效学习因素 ① ( 开始学习 | 高效学习因素五大因素 | 高效学习公式 - 学习效果 = 时间 x 注意力 x 精力 x 目标 x 策略 )
  • LeetCode Medium|【146. LRU 缓存】
  • (南京观海微电子)——LCD OTP(烧录)介绍
  • [E二叉树] lc572. 另一棵树的子树(dfs+前中序判断+树哈希+树上KMP+好题)
  • C# 设计模式之简单工厂模式
  • mac中dyld[5999]: Library not loaded: libssl.3.dylib解决方法
  • python 容器
  • 微信小程序中Component中如何监听属性变化
  • 【Python 逆向滑块】(实战五)逆向滑块,并实现用Python+Node.js 生成滑块、识别滑块、验证滑块、发送短信
  • 微服务架构设计的最佳实践
  • 样式与特效(3)——实现一个测算页面
  • 芯片制造过程4光刻机
  • Nexus3 Repository代理pypi设置与应用
  • PMP–知识卡片--燃起图
  • 63 epoll服务器 (ET模式)
  • AI Agent
  • select
  • 按照指定格式打印pprint()
  • Study--Oracle-07-ASM常用维护操作(五)
  • [Git][分支管理][上]详细讲解
  • C语言指针(1)
  • C语言中的指针与数组
  • CentOS7.9升级OpenSSL1.1.1w
  • 环境搭建:如何安装和使用 MySQL Connector/J——与 MySQL Community Server 的关系