图论题总结

2024-09-03 20:36
文章标签 总结 论题

本文主要是介绍图论题总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

图论总结

  • hot100
    • 岛屿数量
    • 腐烂的橘子
    • 课程表
    • 实现 Trie (前缀树)

hot100

岛屿数量

题目链接:
200.岛屿数量
代码:

class Solution {boolean[][] visited;int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};public void bfs(char[][] grid, int i, int j){Queue<int[]> deque = new LinkedList<>();deque.offer(new int[]{i,j});visited[i][j] = true;while(!deque.isEmpty()){int[] curr = deque.poll();int m = curr[0];int n = curr[1];for (int index = 0; index < 4; index ++){int nexti = m + move[index][0];int nextj = n + move[index][1];if (nexti < 0 || nexti >= grid.length || nextj < 0 || nextj >= grid[0].length) continue;if (!visited[nexti][nextj] && grid[nexti][nextj] == '1'){deque.offer(new int[]{nexti,nextj});visited[nexti][nextj] = true;}}} }public int numIslands(char[][] grid) {int res = 0;visited = new boolean[grid.length][grid[0].length];for (int i = 0; i < grid.length; i ++){for (int j = 0; j < grid[0].length; j ++){if (grid[i][j] == '1' && !visited[i][j]){res ++;bfs(grid,i,j);}}}return res;}
}

腐烂的橘子

题目链接:
994.腐烂的橘子
代码:

class Solution {int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};public int orangesRotting(int[][] grid) {int row = grid.length, col = grid[0].length;Queue<int[]> queue = new LinkedList<>();Map<int[], Integer> depth = new HashMap<>();for (int r = 0; r < row; r ++) {for (int c = 0; c < col; c ++) {if (grid[r][c] == 2) {int[] code = new int[]{r,c};queue.offer(code);depth.put(code, 0);}}}int ans = 0;while (!queue.isEmpty()) {int[] curr = queue.poll();int m = curr[0];int n = curr[1];for (int k = 0; k < 4; k ++) {int next_m = m + move[k][0];int next_n = n + move[k][1];if (0 <= next_m && next_m < row && 0 <= next_n && next_n < col && grid[next_m][next_n] == 1) {grid[next_m][next_n] = 2;int[] next_code = new int[]{next_m,next_n};queue.offer(next_code);depth.put(next_code, depth.get(curr) + 1);ans = depth.get(next_code);}}}for (int r = 0; r < row; r ++) {for (int c = 0; c < col; c ++) {if (grid[r][c] == 1) {return -1;}}}return ans;}
}

课程表

题目链接:
207.课程表
代码:

class Solution {List<List<Integer>> edges;boolean valid = true;int[] visited;public void dfs(int i){visited[i] = 1;for (int edge:edges.get(i)){if (visited[edge] == 0){dfs(edge);if (! valid) return;}else if (visited[edge] == 1){valid = false;return;}}visited[i] = 2;}public boolean canFinish(int numCourses, int[][] prerequisites) {edges = new ArrayList<>();for (int i = 0; i < numCourses; i ++){edges.add(new ArrayList<>());}for (int[] item : prerequisites){edges.get(item[1]).add(item[0]);}visited = new int[numCourses];for (int i = 0; i < numCourses; i ++){if (visited[i] == 0){dfs(i);}}return valid;}
}

实现 Trie (前缀树)

题目链接:
208.实现 Trie (前缀树)
代码:

class Trie {private Trie[] children;private boolean isEnd;public Trie() {children = new Trie[26];isEnd = false;}private Trie searchPrefix(String prefix){Trie node = this;for (int i = 0; i < prefix.length(); i ++){char c = prefix.charAt(i);int index = c - 'a';if (node.children[index] == null) return null;node = node.children[index];}return node;}public void insert(String word) {Trie node = this;for (int i = 0; i < word.length(); i ++){char c = word.charAt(i);int index = c - 'a';if (node.children[index] == null){node.children[index] = new Trie();}node = node.children[index];}node.isEnd = true;}public boolean search(String word) {Trie node = searchPrefix(word);return node != null && node.isEnd;}public boolean startsWith(String prefix) {return searchPrefix(prefix) != null;}
}

这篇关于图论题总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio