323. Number of Connected Components in an Undirected Graph
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的更多相关文章
- 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 ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
- [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), ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- [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), ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [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 ...
- [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), ...
随机推荐
- ListPreference之entries和entryValues
在使用PreferenceActivity时,碰到配置文件的ListPreference有两个属性android:entries,android:entryValues.这两个属性其实就和html的o ...
- Html table 实现Excel多格粘贴
Html table 实现Excel多格粘贴 电商网站的后台总少不了各种繁杂数据的录入,旁边的运营妹子录完第138条商品的时候,终于忍不住转身吼到:为什么后台的录入表不能像Excel那样多行粘贴!!! ...
- struts-json
Struts2序列化的属性,该属性在action中必须有对应的getter方法 如果action的属性很多,我们想要从Action返回到调用页面的数据.这个时候配置includeProperties或 ...
- 获得SQLSERVER的表字段等架构信息
获得SQLSERVER的表字段等架构信息 SELECT 表名 = CASE WHEN A.COLORDER=1 THEN D.NAME ELSE '' END, 表說明 = CASE WHEN A.C ...
- 『重构--改善既有代码的设计』读书笔记----Replace Array with Object
如果你有一个数组,其中的元素各自代表不同东西,比如你有一个 QList<QString> strList; 其中strList[0]代表选手姓名,strList[1]代表选手家庭住址,很显 ...
- python库tkinter、pygame中几点需要注意的问题
恍然之间已经16年快四月份了,已经好久都没有写过东西了.. 最近在用python做一些小的游戏,在网上找了一些Python库,Python中游戏编程最常用的还是pygame了,其次是Tkinter p ...
- void (*f(int, void (*)(int)))(int) 函数解析 转
今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往往使初学者 ...
- apache301重定向设置
<VirtualHost ip地址> #DocumentRoot /文件夹/ ServerName XXXX.com RewriteEngine on RewriteRule ^ ...
- Hbase Java API程序设计步骤
http://www.it165.net/admin/html/201407/3390.html 步骤1:创建一个Configuration对象 包含了客户端链接Hbase服务所需的全部信息: zoo ...
- linux socket使用经验总结
1. scoket函数中PF_INET AF_INET区别 在UNIX系列中,PF_INET表示poxis, BSD系列用AF_INET 2. in_addr_t inet_addr(const ...