图论 day1

java 复制代码
class Solution {
    List<List<Integer>> result;
    List<Integer> path;
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        path = new LinkedList<>();
        result = new LinkedList<>();
        path.add(0);
        dfs(graph,0);
        return result;
    }
    public void dfs(int[][] graph, int node){
        //当前节点是最后一个节点
        if (node==graph.length-1){
            result.add(new LinkedList<>(path));
            return;
        }
        // 循环该节点的各条路径
        for (int index=0;index<graph[node].length;index++){
            int nextNode = graph[node][index];
            path.add(nextNode);
            dfs(graph,nextNode);
            path.removeLast();
        }
    }
}
java 复制代码
class Solution {
    public int numIslands(char[][] grid) {
        int result = 0;
        //遍历网格,找到岛屿(即:'1')
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[0].length;j++){
                if (grid[i][j]=='1'){
                    result++;
                    dfs(grid,i,j);
                }
            }
        }
        return result;
    }
    // 将找到的一个岛屿"淹没",同时对其相连的岛屿淹没,因为这属于同一片岛屿
    public void dfs(char[][] grid, int i, int j){
        if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') return;
        grid[i][j] = '0';
        dfs(grid,i-1,j);
        dfs(grid,i+1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);
    }
}
java 复制代码
class Solution {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;

        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1') {
                    ++num_islands;
                    grid[r][c] = '0';
                    Queue<Integer> neighbors = new LinkedList<>();
                    neighbors.add(r * nc + c);
                    while (!neighbors.isEmpty()) {
                        int id = neighbors.remove();
                        int row = id / nc;
                        int col = id % nc;
                        if (row - 1 >= 0 && grid[row-1][col] == '1') {
                            neighbors.add((row-1) * nc + col);
                            grid[row-1][col] = '0';
                        }
                        if (row + 1 < nr && grid[row+1][col] == '1') {
                            neighbors.add((row+1) * nc + col);
                            grid[row+1][col] = '0';
                        }
                        if (col - 1 >= 0 && grid[row][col-1] == '1') {
                            neighbors.add(row * nc + col-1);
                            grid[row][col-1] = '0';
                        }
                        if (col + 1 < nc && grid[row][col+1] == '1') {
                            neighbors.add(row * nc + col+1);
                            grid[row][col+1] = '0';
                        }
                    }
                }
            }
        }

        return num_islands;
    }
}
相关推荐
Pluto_CSND5 分钟前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
百***46451 小时前
Java进阶-在Ubuntu上部署SpringBoot应用
java·spring boot·ubuntu
serve the people1 小时前
Prompts for Chat Models in LangChain
java·linux·langchain
一叶飘零_sweeeet2 小时前
不止于 API 调用:解锁 Java 工具类设计的三重境界 —— 可复用性、线程安全与性能优化
java·工具类
cynicme2 小时前
力扣3228——将 1 移动到末尾的最大操作次数
算法·leetcode
熬了夜的程序员2 小时前
【LeetCode】109. 有序链表转换二叉搜索树
数据结构·算法·leetcode·链表·职场和发展·深度优先
随意起个昵称2 小时前
【递归】二进制字符串中的第K位
c++·算法
mjhcsp3 小时前
C++ 循环结构:控制程序重复执行的核心机制
开发语言·c++·算法
立志成为大牛的小牛3 小时前
数据结构——四十一、分块查找(索引顺序查找)(王道408)
数据结构·学习·程序人生·考研·算法
A阳俊yi3 小时前
Spring Data JPA
java·开发语言