【LeetCode Hot100】图论篇

前言

本文用于整理LeetCode Hot100中题目解答,因题目比较简单且更多是为了面试快速写出正确思路,只做简单题意解读和一句话题解方便记忆。但代码会全部给出,方便大家整理代码思路。


200. 岛屿数量

一句话题意

求所有上下左右的'1'的连通块数量。

一句话题解

DFS or BFS 搜一下就行了。

java 复制代码
class Solution {
    int[][] fx = {{1,0},{0,1},{-1,0},{0,-1}};
    int n;
    int m;
    char[][] grid;
    void dfs(int x,int y){
        grid[x][y]='0';
        for(int i=0;i<4;i++){
            int xx=x+fx[i][0];
            int yy=y+fx[i][1];
            if(xx<0||xx>=n||yy<0||yy>=m||grid[xx][yy]=='0')continue;
            dfs(xx,yy);
        }
    }
    public int numIslands(char[][] grid) {
        this.grid=grid;
        int ans=0;
        n=grid.length;
        m=grid[0].length;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(grid[i][j]=='1'){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        return ans;
    }
}

994. 腐烂的橘子

一句话题意

给定一个二维数组,二维数组上的每个2为一个烂掉的橘子,1为正常橘子,0为空位。每个坏橘子会每秒向周围四个方向腐烂好的橘子,空位不能传播,问最少多少时间全坏。

一句话题解

多源点广搜,将所有坏的橘子放进去,没搜到一个好的橘子就让他变坏,然后接着搜即可。

java 复制代码
class Solution {
    class Node {
        int x,y,t;
        Node(int x,int y,int t){
            this.x=x;
            this.y=y;
            this.t=t;
        }
    }
    public int orangesRotting(int[][] grid) {
        Queue<Node> q = new LinkedList<>();
        int ans=0;
        int sum=0;
        int n=grid.length;
        int m=grid[0].length;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(grid[i][j]==2)q.add(new Node(i,j,0));
                else if(grid[i][j]==1)sum++;
            }
        }
        int[][] fx={{1,0},{0,1},{-1,0},{0,-1}};
        while(q.size()>0){
            Node o = q.poll();
            ans=Math.max(ans,o.t);
            if(sum==0)continue;
            for(int i=0;i<4;i++){
                int xx=o.x+fx[i][0];
                int yy=o.y+fx[i][1];
                if(xx<0||xx>=n||yy<0||yy>=m||grid[xx][yy]!=1)continue;
                grid[xx][yy]=0;
                sum--;
                q.add(new Node(xx,yy,o.t+1));
            }
        }
        if(sum!=0)ans=-1;
        return ans;
    }
}

207. 课程表

一句话题意

给定一些课程的前后学习关系,问是否能全部学习。

一句话题解

拓扑排序。

java 复制代码
class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        List<List<Integer>> to = new ArrayList<>();
        int[] in = new int[numCourses];
        for (int i = 0; i < numCourses; i++)
            to.add(new ArrayList<>());
        for (int[] a : prerequisites) {
            to.get(a[1]).add(a[0]);
            in[a[0]]++;
        }
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if (in[i] == 0)
                q.add(i);
        }
        while (q.size() > 0) {
            int x = q.poll();
            numCourses--;
            for (Integer y : to.get(x)) {
                in[y]--;
                if (in[y] == 0)
                    q.add(y);
            }
        }
        return numCourses == 0;
    }
}

208. 实现 Trie (前缀树)

一句话题意

请你实现 Trie 类:

  • Trie() 初始化前缀树对象。

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

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

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

一句话题解

实现一棵26岔树。

java 复制代码
class Trie {
    Trie[] children;
    boolean isEnd;
    public Trie() {
        children = new Trie[26];
        isEnd = false;
    }
    
    public void insert(String word) {
        Trie node = this;
        for(char c: word.toCharArray()){
            if(node.children[c-'a'] == null){
                node.children[c-'a'] = new Trie();
            }
            node = node.children[c-'a'];
        }
        node.isEnd = true;
    }
    
    public boolean search(String word) {
        Trie node = this.searchPrefix(word);
        return node!=null&&node.isEnd;
    }
    
    public boolean startsWith(String prefix) {
        return this.searchPrefix(prefix) != null;
    }

    public Trie searchPrefix(String s){
        Trie node = this;
        for(Character c:s.toCharArray()){
            if(node.children[c-'a']==null)return null;
            node=node.children[c-'a'];
        }
        return node;
    }
}
相关推荐
开开心心就好2 小时前
网页图片批量下载工具,输入链接就保存
服务器·算法·leetcode·支持向量机·pdf·ocr·1024程序员节
Navigator_Z3 小时前
LeetCode //C - 1313. Decompress Run-Length Encoded List
c语言·算法·leetcode
dialect_3 小时前
CODEFORCES ROUND 1127 DIV2
开发语言·c++·算法
Nuanyt3 小时前
LeetCode Hot 100 链表专题(四)· 3 题(Java)
java·算法·leetcode·链表·双指针
灯澜忆梦3 小时前
LeetCode 1861. 旋转盒子|Go 双指针解法
算法
软件派4 小时前
生产动态数据智能风控:阈值判别、趋势预测、异常识别算法工程落地实战
前端·数据库·算法
ZhengEnCi12 小时前
模型训练完整流程:从数据准备到模型上线的 10 个关键步骤(全文可跑)
后端·算法
吞下星星的少年·-·16 小时前
Div.3 CF 1131A题(排序,模拟)
算法·模拟·排序
aichitang202416 小时前
代数拓扑学与现代前沿视角
考研·算法·数学建模·ai·拓扑学·高考