力扣labuladong——一刷day81

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


前言


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

一、力扣990. 等式方程的可满足性

class Solution {
   
    public boolean equationsPossible(String[] equations) {
   
        if(equations == null || equations.length == 0){
   
            return true;
        }
        int n = equations.length;
        Uf uf = new Uf(27);
        for(int i = 0; i < n; i ++){
   
            char c1 = equations[i].charAt(1);
            if(c1 == '='){
   
                int x = equations[i].charAt(0) - 'a';
                int y = equations[i].charAt(3) - 'a';
                uf.union(x,y);
            }
        }
        for(int i = 0; i < n; i ++){
   
            if(equations[i].charAt(1) == '!'){
   
                int x = equations[i].charAt(0) - 'a';
                int y = equations[i].charAt(3) - 'a';
                if(uf.getConnection(x, y)){
   
                    return false;
                }
            }
        }
        return true;
    }
    class Uf{
   
        private int count;
        private int[] parent;
        public Uf(int n){
   
            this.count = n;
            this.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 boolean getConnection(int x, int y){
   
            int rootx = find(x);
            int rooty = find(y);
            return rootx == rooty;
        }
        public void union(int x, int  y){
   
            int rootx = find(x);
            int rooty = find(y);
            if(rootx == rooty){
   
                return;
            }
            this.parent[rootx] = rooty;
            count --;
        }
    }
}

相关推荐

  1. labuladong——day81

    2023-12-30 21:14:07       39 阅读
  2. labuladong——day80

    2023-12-30 21:14:07       33 阅读
  3. labuladong——day84

    2023-12-30 21:14:07       35 阅读
  4. labuladong——day87

    2023-12-30 21:14:07       36 阅读
  5. labuladong——day89

    2023-12-30 21:14:07       31 阅读
  6. labuladong——day68

    2023-12-30 21:14:07       37 阅读
  7. labuladong——day67

    2023-12-30 21:14:07       33 阅读
  8. labuladong——day69

    2023-12-30 21:14:07       37 阅读
  9. labuladong——day66

    2023-12-30 21:14:07       33 阅读
  10. labuladong——day70

    2023-12-30 21:14:07       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 21:14:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 21:14:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 21:14:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 21:14:07       18 阅读

热门阅读

  1. LeetCode657. Robot Return to Origin

    2023-12-30 21:14:07       33 阅读
  2. mysql 数据查重与查重分页

    2023-12-30 21:14:07       33 阅读
  3. leetcode541. 反转字符串II

    2023-12-30 21:14:07       37 阅读
  4. react入门笔记

    2023-12-30 21:14:07       30 阅读
  5. KSO-SAP ABAP 创建webservice服务,并用soapui测试

    2023-12-30 21:14:07       33 阅读
  6. vue 页面刷新、重置、更新页面所有数据

    2023-12-30 21:14:07       52 阅读
  7. 算法训练营Day25

    2023-12-30 21:14:07       41 阅读
  8. OpenCV-Python(22):直方图均衡化

    2023-12-30 21:14:07       39 阅读
  9. Python实现进度条

    2023-12-30 21:14:07       39 阅读
  10. ARM12.26

    ARM12.26

    2023-12-30 21:14:07      33 阅读
  11. 项目中cesium使用方法

    2023-12-30 21:14:07       33 阅读
  12. 四、KMDF开发之traceview跟踪打印信息

    2023-12-30 21:14:07       37 阅读
  13. 【Yii2】数据库查询方法总结

    2023-12-30 21:14:07       40 阅读