LeetCode Hot 100 -- 图论

岛屿数量

java 复制代码
class Solution {

    // dfs 模板
    public static int[][] dir = {{0, 1}, {1, 0}, {-1, 0},{0, -1}};
    public static void dfs(boolean[][] visited, int x, int y, char[][] grid) {
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            if (nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) continue;  // 越界
            // 下一个位置没有被访问过,且为 1
            if (!visited[nextX][nextY] && grid[nextX][nextY] == '1') {
                visited[nextX][nextY] = true;  // 标记为已访问
                dfs(visited, nextX, nextY, grid);  // 使用递归搜索相邻的格子
            }
        }
    }

    public int numIslands(char[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int ans = 0;
        boolean[][] visited = new boolean[m][n];
        
        // 解题思路:找到未被标记,且图上为 1 的点,然后找出其周围的所有 1,这就可以看作找到了一个岛屿,继续重复这个操作就可以找到岛屿的数量
        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;
    }
}
java 复制代码
class Solution {
    // 逆时针,下右上左
    public static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 
    static class pair {
        int first, second;
        pair(int x, int y) {
            this.first = x;
            this.second = y;
        }
    }
    public static void bfs(boolean[][] visited, int x, int y, char[][] grid) {
       Queue<pair> queue = new LinkedList<pair>();
       queue.add(new pair(x, y));
       visited[x][y] = true;
       while (!queue.isEmpty()) {
        int curX = queue.peek().first;
        int curY = queue.poll().second;
        for (int i = 0; i < 4; i++) {
            int nextX = curX + dir[i][0];
            int nextY = curY + dir[i][1];
            if (nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) continue;
            if (!visited[nextX][nextY] && grid[nextX][nextY] == '1') {
                // 使用队列来进行 bfs
                queue.add(new pair(nextX, nextY));
                visited[nextX][nextY] = true;
            }
         }
      }
    }

    public int numIslands(char[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int ans = 0;
        boolean[][] visited = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!visited[i][j] && grid[i][j] == '1') {
                    ans++;
                    bfs(visited, i, j, grid);
                }
            }
        }
        return ans;
    }
    
    
}

课程表

java 复制代码
class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        // 构建图
        List<Integer>[] graph = new List[numCourses];  // 图的邻接表
        for (int i = 0; i < numCourses; i++) {
            graph[i] = new ArrayList<>();
        }
        for (int[] pre : prerequisites) {
            graph[pre[1]].add(pre[0]);
        }

        // 用于 DFS 判断环的状态数组
        int[] visited = new int[numCourses];

        // 对每个课程进行 DFS 遍历
        for (int i = 0; i < numCourses; i++) {
            if(hasCycle(graph, visited, i)) {
                return false;
            }
        }
        
        return true;
    }


    private boolean hasCycle(List<Integer>[] graph, int[] visited, int course) {
        if (visited[course] == 1) {
            return true;
        }
        if (visited[course] == 2) {
            return false;
        }

        visited[course] = 1;
        for (int neighbor : graph[course]) {
            if (hasCycle(graph, visited, neighbor)) {
                return true;
            }
        }

        visited[course] = 2;
        
        return false;
    }
}

邻接表:graph = \[, 0, 1 ],表示课程1依赖课程0,课程2依赖课程1

相关推荐
To_OC8 小时前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC8 小时前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode
To_OC1 天前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
To_OC2 天前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
To_OC2 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
To_OC4 天前
LC 49 字母异位词分组:想到哈希表很简单,选对 key 才是精髓
javascript·算法·leetcode
To_OC6 天前
LC 1 两数之和:面试第一道必考题,暴力解法直接被面试官 pass
javascript·算法·leetcode
想吃火锅100512 天前
【leetcode】121.买卖股票的最佳时机js/c++
算法·leetcode·职场和发展
凌波粒12 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
退休倒计时12 天前
【每日一题】LeetCode 146. LRU 缓存 TypeScript
算法·leetcode·缓存·typescript