leetcode-hot100-图论:200岛屿数量-994腐烂的橘子-207课程表-208实现前缀树

200 岛屿数量

题目:给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

此外,你可以假设该网格的四条边均被水包围。

java 复制代码
class Solution {
    public static int[][] path={{0,1},{1,0},{-1,0},{0,-1}};
    public int numIslands(char[][] grid) {
        int m=grid.length;
        int n=grid[0].length;
        boolean[][] visited=new boolean[m][n];
        int ans=0;

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(!visited[i][j] && grid[i][j]=='1'){
                    ans++;
                    visited[i][j]=true;
                    dfs(visited,i,j,grid);
                }
            }
        }
        return ans;
    }

    private void dfs(boolean[][] visited,int x,int y,char[][] grid){
        for(int i=0;i<4;i++){
            int nextx=x+path[i][0];
            int nexty=y+path[i][1];

            if(nextx<0 || nexty<0 ||nextx>=grid.length || nexty>=grid[0].length) continue;
            if(!visited[nextx][nexty] && grid[nextx][nexty]=='1'){
                visited[nextx][nexty]=true;
                dfs(visited,nextx,nexty,grid);
            }
        }
    }
}

994 腐烂的橘子

题目:在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一:值 0 代表空单元格;值 1 代表新鲜橘子;值 2 代表腐烂的橘子。每分钟,腐烂的橘子周围 4 个方向上相邻的新鲜橘子都会腐烂。返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1 。

java 复制代码
class Solution {
    private static final int[][] directions={{-1,0},{1,0},{0,-1},{0,1}};

    public int orangesRotting(int[][] grid) {
        int m=grid.length;
        int n=grid[0].length;
        int fresh=0;

        List<int[]> q=new ArrayList<>();
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]==1){
                    fresh++;
                }else if(grid[i][j]==2){
                    q.add(new int[]{i,j});
                }
            }
        }

        int ans=0;
        while(fresh>0 && !q.isEmpty()){
            ans++;
            List<int[]> tmp=q;
            q=new ArrayList<>();
            for(int[] pos:tmp){
                for(int[] d:directions){
                    int i=pos[0]+d[0];
                    int j=pos[1]+d[1];

                    if(i>=0 && i<m && j>=0 && j<n &&grid[i][j]==1){
                        fresh--;
                        grid[i][j]=2;
                        q.add(new int[]{i,j});
                    }
                }
            }
        }

        return fresh>0?-1:ans;
    }
}

207课程表

题目:你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则必须先学习课程 bi 。例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1。请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。

java 复制代码
class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        List<Integer>[] g=new ArrayList[numCourses];
        Arrays.setAll(g,i->new ArrayList<>());
        for(int[] p:prerequisites){
            g[p[1]].add(p[0]);
        }

        int[] colors=new int[numCourses];
        for(int i=0;i<numCourses;i++){
            if(colors[i]==0 && dfs(i,g,colors)){
                return false;
            }
        }
        return true;
    }

    private boolean dfs(int x,List<Integer>[] g,int[] colors){
        colors[x]=1;
        for(int y:g[x]){
            if(colors[y]==1 || colors[y]==0 && dfs(y,g,colors)){
                return true;
            }
        }
        colors[x]=2;
        return false;
    }
}

208实现前缀树

题目:Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补全和拼写检查。

请你实现 Trie 类:Trie() 初始化前缀树对象。

void insert(String word) 向前缀树中插入字符串 word 。

boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。

boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

java 复制代码
class Trie {
    private static class Node{
        Node[] son=new Node[26];
        boolean end=false;
    }
    private final Node root=new Node();

    public Trie() {
        
    }
    
    public void insert(String word) {
        Node cur=root;
        for(char c:word.toCharArray()){
            c-='a';
            if(cur.son[c]==null){ // 无路可走?
                cur.son[c]=new Node(); // new 出来
            }
            cur=cur.son[c];
        }
        cur.end=true;
    }
    
    public boolean search(String word) {
        return find(word)==2;
    }
    
    public boolean startsWith(String prefix) {
        return find(prefix)!=0;
    }

    private int find(String word){
        Node cur=root;
        for(char c:word.toCharArray()){
            c-='a';
            if(cur.son[c]==null){
                return 0;
            }
            cur=cur.son[c];
        }
        return cur.end? 2:1; // 走过同样的路(2=完全匹配,1=前缀匹配)
    }
}
相关推荐
khalil10201 小时前
代码随想录算法训练营Day-31贪心算法 | 56. 合并区间、738. 单调递增的数字、968. 监控二叉树
数据结构·c++·算法·leetcode·贪心算法·二叉树·递归
谭欣辰2 小时前
C++ 版Dijkstra 算法详解
c++·算法·图论
im_AMBER4 小时前
Leetcode 160 最小覆盖子串 | 串联所有单词的子串
开发语言·javascript·数据结构·算法·leetcode
帅小伙―苏6 小时前
力扣483找到字符串中所有字母异位词
算法·leetcode
smj2302_796826526 小时前
解决leetcode第3906题统计网格路径中好整数的数目
python·算法·leetcode
KobeSacre6 小时前
leetcode 树
算法·leetcode·职场和发展
大大杰哥7 小时前
leetcode hot100(1) 哈希
leetcode
Engineer邓祥浩7 小时前
LeetCode 热题 100 - 第1题:两数之和
算法·leetcode·职场和发展
阿Y加油吧8 小时前
算法二刷复盘:LeetCode 79 单词搜索 & 131 分割回文串(Java 回溯精讲)
java·算法·leetcode
6Hzlia8 小时前
【Hot 100 刷题计划】 LeetCode 101. 对称二叉树 | C++ DFS 极简递归模板
c++·leetcode·深度优先