May-26-2019

UF做算是白给的

压缩下,没Quick union(否则要几把weight),然后比较integer用equals稳点= =顺便尽量别int[]能MAP最好MAP

class Solution {

    public class UF {
Map<Integer, Integer> roots;
Map<Integer, Integer> size;
int num;
public UF(int n) {
roots = new HashMap<>();
size = new HashMap<>();
num = n;
for (int i = 0; i < n; i ++) {
roots.put(i, i);
size.put(i, 1);
}
} public Integer find(Integer n) {
if (!roots.containsKey(n)) return null;
Integer root = n;
while (!root.equals(roots.get(root))) {
roots.put(root, roots.get(roots.get(root)));
root = roots.get(root);
}
return root;
} public void union(Integer a, Integer b) {
Integer rootA = find(a);
Integer rootB = find(b); if (rootA == null || rootB == null || rootA.equals(rootB)) return; Integer sizeA = size.get(rootA);
Integer sizeB = size.get(rootB);
if (sizeA > sizeB) {
roots.put(rootB, rootA);
size.put(rootA, sizeB + sizeA);
} else {
roots.put(rootA, rootB);
size.put(rootB, sizeA + sizeB);
}
this.num --;
} }
public int countComponents(int n, int[][] edges) {
if (edges.length == 0 || edges[0].length == 0) return n; UF uf = new UF(n);
for (int[] edge : edges) {
Integer a = edge[0];
Integer b = edge[1];
uf.union(a, b);
}
return uf.num;
}
}

323. Number of Connected Components in an Undirected Graph的更多相关文章

  1. 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集

    [抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...

  2. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  3. 323. Number of Connected Components in an Undirected Graph (leetcode)

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. [LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  5. 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  6. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  7. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  8. [Locked] Number of Connected Components in an Undirected Graph

    Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...

  9. [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

随机推荐

  1. 原生与jqueryDOM

    总结与复习原生与jquery的DOM操作. 获取元素节点: $(".class") $("#id") $(".class div") $(& ...

  2. 【工具篇】source Insight

    不多说,阅读代码利器. 一.修改背景颜色 使用淡绿色更护眼(听说而已),菜单“选项”>>“属性”,使用自己喜欢的颜色吧.我的淡绿色RGB是181,236,207 二.行号,空格替换tabs ...

  3. centos下配置多个tomcat同时运行

    首先安装好jdk,下载好tomcat,我的是apache-tomcat-7.0.50,不用专门配置CATALINA_2_BASE,CATALINA_2_HOME等环境变量. 把tomcat解压到lin ...

  4. ubuntu安装配置搜狗拼音输入法

    进入下载目录,在终端执行安装 $sudo dpkg  -i   sogou_pinyin_linux_1.0.0.0033_amd64.deb 安装过程会出现 依赖关系问题 2 修复依赖关系完成搜狗拼 ...

  5. .net中String是引用类型还是值类型 以及 C#深层拷贝浅层拷贝

    http://www.cnblogs.com/yank/archive/2011/10/24/2204145.html http://www.cnblogs.com/zwq194/archive/20 ...

  6. 添加标签2 jquery 和JS

    TAG添加标签 做了个方法方便调用 一.JS版本 <!DOCTYPE html> <html lang="en"> <head> <met ...

  7. table 添加右键,并获取选中行信息

    import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java. ...

  8. python的py文件打包成exe

    一.首先需要安装Pyinstaller-- 使用pip来安装模块 (我电脑上装的是python的一个编译环境Anaconda,如果电脑上装的是python自带的IDE的话,就直接进入python的安装 ...

  9. pyqt5 窗体布局

    窗体布局 1使用qtdesigner新建一个对话框,然后拖放几个按钮和文本框,按钮使用水平布局,效果如下: 鼠标选中水平布局再选中文本框,进行垂直布局,如下: 垂直布局后的效果如下: 然后,如何让窗体 ...

  10. Mac下修改Mysql密码

    1. 停止Mysql 2. cd /usr/local/mysql/bin/ 3. 使用跳过权限方式启动mysql sudo ./mysqld_safe --skip-grant-tables &am ...