intitlt 山西大同网站建设wordpress rightlock
intitlt 山西大同网站建设,wordpress rightlock,保定网络运营公司,阿里云预安装wordpress目录
题目链接
岛屿数量思路及其代码
代码如下
腐烂的橘子思路及其代码
注意事项
代码
课程表的思路及其代码
注意事项
代码
前缀树的思路及其代码
思路
代码 题目链接
200. 岛屿数量 - 力扣#xff08;LeetCode#xff09;
994. 腐烂的橘子 - 力扣#xff08…目录题目链接岛屿数量思路及其代码代码如下腐烂的橘子思路及其代码注意事项代码课程表的思路及其代码注意事项代码前缀树的思路及其代码思路代码题目链接200. 岛屿数量 - 力扣LeetCode994. 腐烂的橘子 - 力扣LeetCode207. 课程表 - 力扣LeetCode208. 实现 Trie (前缀树) - 力扣LeetCode其中简单分个类 岛屿数量是FloodFill洪水灌溉算法专题腐烂的橘子是多源BFS专题课程表是拓扑排序专题前缀树是一种数据结构岛屿数量思路及其代码其实思路都是大同小异的。不过我提一提我的细节处理部分。排除已经遍历过的岛屿的方法引入向量数组去处理4个方向代码如下class Solution { int[] dx{0,0,1,-1}; int[] dy{1,-1,0,0}; int size0; boolean[][] visit; public int numIslands(char[][] grid) { Queueint[] queuenew LinkedList(); int mgrid.length; int ngrid[0].length; visitnew boolean[m][n]; for(int i0;im;i){ for(int j0;jn;j){ if(grid[i][j]1visit[i][j]false){ queue.offer(new int[]{i,j}); // grid[i][j]0; visit[i][j]true; while(!queue.isEmpty()){ int[] tqueue.poll(); int at[0]; int bt[1]; for(int h0;h4;h){ int xadx[h]; int ybdy[h]; // if(x0xmy0yngrid[x][y]1){ if(x0xmy0ynvisit[x][y]falsegrid[x][y]1){ queue.offer(new int[]{x,y}); //将已经遍历过的修改为0 // grid[x][y]0; visit[x][y]true; } } } size; } } } return size; } }腐烂的橘子思路及其代码思路还是同岛屿数量代码都长得差不多.注意事项怎么判断是否还有新鲜橘子呢注意一个烂橘子同时腐烂周围的橘子算1次如果有两个烂橘子分别同时腐烂周围的橘子也算一次所以说引入queue.size()和is_Infected就很重要。代码class Solution { int fresh0; boolean[][] visit; int[] dx{0,0,1,-1}; int[] dy{1,-1,0,0}; boolean isInfectedfalse; int minute0; public int orangesRotting(int[][] grid) { int mgrid.length; int ngrid[0].length; visitnew boolean[m][n]; //先统计所有新鲜的橘子数 for(int i0;im;i){ for(int j0;jn;j){ if(grid[i][j]1){ fresh; } // fresh; } } if(fresh0){ return 0; } Queueint[] queuenew LinkedList(); //先找到所有腐烂的橘子然后加入queue种 for(int i0;im;i){ for(int j0;jn;j){ if(grid[i][j]2){ queue.offer(new int[]{i,j}); visit[i][j]true; } } } while(!queue.isEmpty()){ //因为可能一次有多个腐烂橘子加入队列 同时是腐烂周围的橘子本质上都是算一分钟 int sizequeue.size(); for(int i0;isize;i){ int[] tqueue.poll(); int at[0]; int bt[1]; for(int h0;h4;h){ int xadx[h]; int ybdy[h]; if(x0xmy0yngrid[x][y]1visit[x][y]false){ queue.offer(new int[]{x,y}); visit[x][y]true; fresh--; isInfectedtrue; } } } if(isInfected){ minute; //记得还原 isInfectedfalse; } } return fresh0?-1:minute; } }课程表的思路及其代码首先解决这道题你需要直到什么是拓扑排序。本质就是判断图是否有环即可注意事项怎么去建图我认为很关键代码class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { int mprerequisites.length; // int nprerequisites[0].length; //统计入度 int[] innew int[numCourses]; //建图 MapInteger,ListInteger mapnew HashMap(); for(int i0;im;i){ int aprerequisites[i][0]; int bprerequisites[i][1]; //关系是b-a if(!map.containsKey(b)){ map.put(b,new ArrayList()); } map.get(b).add(a); in[a]; } //进行拓扑排序 //进行BFS找到所有入度为0的放入队列 QueueInteger queuenew LinkedList(); for(int i0;inumCourses;i){ if(in[i]0){ queue.offer(i); } } while(!queue.isEmpty()){ int tqueue.poll(); //删除与入度为0的点相连的边 for(int x:map.getOrDefault(t,new ArrayList())){ in[x]--; if(in[x]0){ queue.offer(x); } } } for(int p:in){ if(p!0){ return false; } } return true; } }前缀树的思路及其代码这道题我第一次写的时候有点浮躁看题解没看懂。今天在一次写的时候突然看懂了.主要是看的灵神的题解思路insert的具体插入图插入apple代码class Trie { public static class Node{ Node[] sonnew Node[26]; boolean endfalse; } public Node rootnew Node(); public void insert(String word) { Node curroot; for(char c:word.toCharArray()){ int mc-a; if(cur.son[m]null){ cur.son[m]new Node(); } curcur.son[m]; } cur.endtrue; } public boolean search(String word) { return find(word)2; } public boolean startsWith(String prefix) { return find(prefix)!0; } public int find(String word){ Node curroot; for(char c:word.toCharArray()){ int mc-a; if(cur.son[m]null){ return 0; } curcur.son[m]; } //返回2为完全匹配 返回1为前缀匹配 return cur.end?2:1; } } /** * Your Trie object will be instantiated and called as such: * Trie obj new Trie(); * obj.insert(word); * boolean param_2 obj.search(word); * boolean param_3 obj.startsWith(prefix); */