力扣labuladong——一刷day80

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


并查集(Union-Find)算法是一个专门针对「动态连通性」的算法,我之前写过两次,因为这个算法的考察频率高,而且它也是最小生成树算法的前置知识,所以我整合了本文,争取一篇文章把这个算法讲明白。

一、力扣323.无向图中连通分量的数目

class UF{
   
	private int[]parent;
	private int count;
	public UF(int n){
   
		parent = new int[n];
		count = n;
		for(int i = 0; i < n; i ++){
   
			parent[i] = i;
		}
	}
	public int getCount(){
   
		return count;
	}
	public int find(int x){
   
		while(parent[x] != x){
   
			parent[x] = find(parent[x]);
		}
		return parent[x];
	}
	public void union(int x, int y){
   
		int rootX = find(x);
		int rootY = find[y];
		if(rootX == rooY){
   
			return;
		}
		parent[rootX] = rootY;
		count --;
	}
	public boolean getConnection(int x, int y){
   
		int rootX = find(x);
		int rootY = find(y);
		return rootX == rootY;
	}
	public int fun(int n, int[][] edges){
   
		UF uf = new UF(n);
		for(int i = 0; i < n; i ++){
   
			union(edges[i][0], edges[i][1]);
		}
		return getCount();
	}
}

二、力扣130. 被围绕的区域

class Solution {
   
    public void solve(char[][] board) {
   
        int[][] arr = new int[][]{
   
            {
   0,1},
            {
   0,-1},
            {
   -1,0},
            {
   1,0}
        };
        if(board == null || board.length == 0){
   
            return;
        }
        int m = board.length;
        int n = board[0].length;
        int dummy = m * n;
        UF uf = new UF(dummy+1);
        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);
            }
        }
        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);
            }
        }
        for(int i = 1; i < m-1; i ++){
   
            for(int j = 1; j < n-1; j ++){
   
                if(board[i][j] == 'O'){
   
                    for(int k = 0; k < 4; k ++){
   
                        int X = i + arr[k][0];
                        int Y = j + arr[k][1];
                        if(board[X][Y] == 'O'){
   
                            uf.union(X*n+Y,i*n+j);
                        }
                    }
                }
            }
        }
        for(int i = 0; i < m; i ++){
   
            for(int j = 0; j < n; j ++){
   
                if(board[i][j] == 'O' && !uf.getConnection(i*n+j, dummy)){
   
                    board[i][j] = 'X';
                }
            }
        }
    }
    class UF{
   
        private int count;
        private int[] parent;
        public UF(int n){
   
            count = n;
            parent = new int[n];
            for(int i = 0; i < n; i ++){
   
                parent[i] = i;
            }
        }
        public int getCount(){
   
            return count;
        }
        public int find(int x){
   
            if(parent[x] != x){
   
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }
        public void union(int x, int y){
   
            int rootX = find(x);
            int rootY = find(y);
            if(rootX == rootY){
   
                return;
            }
            parent[rootX] = rootY;
            count --;
        }
        public boolean getConnection(int x, int y){
   
            int rootX = find(x);
            int rootY = find(y);
            return rootX == rootY;
        }
    }
}

相关推荐

  1. labuladong——day80

    2023-12-28 22:38:05       57 阅读
  2. labuladong——day81

    2023-12-28 22:38:05       58 阅读
  3. labuladong——day84

    2023-12-28 22:38:05       53 阅读
  4. labuladong——day87

    2023-12-28 22:38:05       58 阅读
  5. labuladong——day89

    2023-12-28 22:38:05       53 阅读
  6. labuladong——day68

    2023-12-28 22:38:05       56 阅读
  7. labuladong——day67

    2023-12-28 22:38:05       60 阅读
  8. labuladong——day69

    2023-12-28 22:38:05       56 阅读
  9. labuladong——day66

    2023-12-28 22:38:05       51 阅读
  10. labuladong——day70

    2023-12-28 22:38:05       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 22:38:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 22:38:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 22:38:05       82 阅读
  4. Python语言-面向对象

    2023-12-28 22:38:05       91 阅读

热门阅读

  1. 面试问题整理若干

    2023-12-28 22:38:05       60 阅读
  2. 观察者(模板)的一点体会

    2023-12-28 22:38:05       49 阅读
  3. 大数据知识分享:大数据产业必知概念

    2023-12-28 22:38:05       63 阅读
  4. SQL备忘--子查询与ALL/ANY运算符

    2023-12-28 22:38:05       57 阅读
  5. 幸运树。。

    2023-12-28 22:38:05       53 阅读
  6. Oracle中varchar2和nvarchar2的区别

    2023-12-28 22:38:05       57 阅读
  7. Qt中保存和还原Widget状态的入门指南

    2023-12-28 22:38:05       68 阅读
  8. Python 虚拟环境工具及使用总结

    2023-12-28 22:38:05       49 阅读
  9. C语言中的Strict Aliasing Rule

    2023-12-28 22:38:05       57 阅读
  10. Python常用命令

    2023-12-28 22:38:05       58 阅读
  11. 面试官:BIO、NIO、AIO的区别

    2023-12-28 22:38:05       52 阅读