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

图论题总结

图论总结

  • 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.mrgr.cn/news/18922.html

相关文章:

  • 数据结构————单向链表
  • Kettle 锁表原因及解决办法【源码级分析】
  • Python数据分析实战,兰州市二手房市场深度分析
  • 4款电脑数据恢复软件分享,告别天价恢复!
  • Pyspark中catalog的作用与常用方法
  • Java中的双亲委派模型以及如何破坏双亲委派
  • Elasticsearch检索原理
  • java:线程池
  • AI预测体彩排3采取888=3策略+和值012路或胆码测试9月3日升级新模型预测第71弹
  • Mysql(一) - 数据库操作, 表操作, CRUD
  • python使用selenium,实现简单爬虫功能
  • 什么是网络准入控制系统?四款网络准入控制系统推荐 干货满满!
  • 在K8s上运行GitHub Actions的自托管运行器
  • 计算机网络——ARP篇
  • 让AI给你写代码(10.1): 按接口编程的思想,统一利用内部和外部的接口,逐步扩展和提升AI编程能力
  • Azure和Transformers的详细解释
  • FastAPI 中间件与依赖注入:打造灵活的 API 架构
  • Web前端全栈Vue3项目实战:‌从零到一的完整指南
  • 仕考网:事业单位考试分为哪几种类型?
  • Python | Pandas中有效处理大数据集的6种方法