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 给出,其中 prerequisitesi = 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=前缀匹配)
    }
}
相关推荐
剑挑星河月37 分钟前
35.搜索插入位置
java·数据结构·算法·leetcode
闪电悠米1 小时前
力扣hot100-438.找到字符串中所有字母异位词-固定长度滑动窗口详解
linux·服务器·数据结构·算法·leetcode·滑动窗口·力扣hot100
小陈的代码之路13 小时前
回文链表(LeetCode 234)C语言最佳解题思路
c语言·leetcode·链表
郭梧悠16 小时前
算法:有效的括号
python·算法·leetcode
旖-旎16 小时前
《LeetCode 1137 第N个泰波那契数 和 LeetCode 三步问题》
c++·算法·leetcode·动态规划
wabs66616 小时前
关于动态规划【力扣718.最长重复子数组的思考】
算法·leetcode·动态规划
YuK.W18 小时前
Leetcode100: 94.二叉树中序遍历、104.二叉树最大深度、226.翻转二叉树
java·算法·leetcode·二叉树
想吃火锅10051 天前
【leetcode】146.LRU缓存js
算法·leetcode·缓存
To_OC6 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC6 天前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode