• 130. 被围绕的区域(Java、Union-Find)


    另类的思路:并查集Union-Find
    将边界上的‘O’与dummy连接,再对内部的‘O’进行上下左右查找连接
    最后遍历查找所有与dummy不相连的元素‘O’,替换成‘X’
    详情见注释

    class Solution {
        //Union-Find的思路
        void solve(char[][] board) {
            if (board.length == 0) {
                return;
            }
            int m = board.length;
            int n = board[0].length;
            // 给 dummy 留一个额外位置
            UF uf = new UF(n*m+1);
            int dummy = n*m;
            // 将首列和末列的 O 与 dummy 连通
            for(int i = 0;i<m;i++){
                if(board[i][0] == 'O'){
                    uf.union(i*n,dummy);
                }
                if(board[i][n-1] == 'O'){
                    uf.union(i*n+n-1,dummy);
                }
            }
            // 将首行和末行的 O 与 dummy 连通
            for(int i = 0;i<n;i++){
                if(board[0][i] == 'O'){
                    uf.union(i,dummy);
                }
                if(board[m-1][i] == 'O'){
                    uf.union((m-1)*n+i,dummy);
                }
            }
            // 方向数组 d 是上下左右搜索的常用手法
            int[][] d = new int[][]{{1,0},{0,1},{0,-1},{-1,0}};
            for(int i = 1;i<m-1;i++){
                for(int j = 1;j<n-1;j++){
                    if(board[i][j] == 'O'){
                        // 将此 O 与上下左右的 O 连通
                        for(int k = 0;k<4;k++){
                            int x = i + d[k][0];
                            int y = j + d[k][1];
                            if(board[x][y] == 'O'){
                                uf.union(i*n+j,x*n+y);
                            }
                        }
                    }
                }
            }
            // 所有不和 dummy 连通的 O,都要被替换
            for(int i = 1;i<m;i++){
                for(int j = 1;j<n;j++){
                    if(!uf.connection(i*n+j,dummy)){
                        board[i][j] = 'X';
                    }
                }
            }
    	}
    }
    class UF {
        // 连通分量个数
        private int count;
        // 存储每个节点的父节点
        private int[] parent;
    
        // n 为图中节点的个数
        public UF(int n) {
            this.count = n;
            parent = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
            }
        }
        
        // 将节点 p 和节点 q 连通
        public void union(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            
            if (rootP == rootQ)
                return;
            
            parent[rootQ] = rootP;
            // 两个连通分量合并成一个连通分量
            count--;
        }
    
        // 判断节点 p 和节点 q 是否连通
        public boolean connected(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            return rootP == rootQ;
        }
    	//每次调用 find 函数向树根遍历的同时,顺手就将树高缩短了
        public int find(int x) {
            if (parent[x] != x) {
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }
    
        // 返回图中的连通分量个数
        public int count() {
            return count;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
  • 相关阅读:
    二战字节跳动成功上岸,准备了小半年,要个28k应该不过分吧~
    【图解HTTP】HTTP首部
    生产环境 SSH 安全有效小技巧
    YOLOv8-Seg改进:多尺度空洞注意力(MSDA),增强局部、稀疏提取特征能力
    微服务分布式开源架构是什么?
    【Oracle数据库CRS-2878: Failed to restart resource ‘ora.chad‘】
    【Vue】ref引用,插槽
    关于Vue的生命周期
    数据库分区的通俗解释
    华为手机的备忘录,你真的会用吗?这4个小功能,很多人都不会用
  • 原文地址:https://blog.csdn.net/weixin_54678689/article/details/125444230