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), ...
随机推荐
- 7-1 DBA顾问培训内容@20141230
1, 逻辑读还是物理读? 查询语句的实际执行计划. F5 预计执行计划. --如何产生实际执行计划 ??. --Session收集指令. workload repository report fo ...
- 3D Game Programming with directx 11 习题答案 8.3
第八章 第三题 1.将flare.dds和flarealpha.dds拷贝到工程目录 2.创建shader resource view HR(D3DX11CreateShaderResourceVie ...
- ACM HDU 2674 N! Again(数论)
继续数论.. Problem Description WhereIsHeroFrom: Zty,what are you doing ? Zty: ...
- 理解ThreadLocal背后的概念
介绍 我之前在任何场合都没有使用过thread local,因此没有注意到它,直到最近用到它的时候. 前提信息 线程可以理解为一个单独的进程,它有自己的调用栈.在java中每一个线程都有一个调用栈或者 ...
- sharepoint的webpart开发
前言 以前没有接触sharepoint感觉这东西好陌生,只是知道.来公司这段时间,也没有参加开发.今天自己简单的实现了一下这个开发过程,webpart部分的. 过程 其实webpart可以理解为一个放 ...
- go和swift
你生命中的有些东西终究会失去,比如我住了6年的陈寨,这个聚集了郑州十几万IT民工的地方,说拆就拆了.再比如我玩了3年的坦克英雄,这个带给我太多快乐的游戏,说停就停了. 编程对我而言是种爱好,我上学6年 ...
- Linux Kernel Makefile Test
一.本文说明 本文为linux内核Makefile整体分析的续篇,是依据Linux内核Makefile体系的主要内容编写一个简要的测试工程.Linux内核Makefile体系就好像一只“大鸟”,而这篇 ...
- 虚拟机linux下使用cuteftp
操作系统 redhat9.0 一.主机配置 1.查看主机是否安装了ftp服务器,如果没有,安装它 在终端上输入setup,在弹出的界面中选择system services.查看弹出的界面中是否有vs ...
- Java 高效检查一个数组中是否包含某个值
如何检查一个数组(未排序)中是否包含某个特定的值?在Java中,这是一个非常有用并又很常用的操作.同时,在StackOverflow中,有时一个得票非常高的问题.在得票比较高的几个回答中,时间复杂度差 ...
- Map迭代器
今天用到了,发现不会,随手谷歌之,整理如下. //Map是接口,刚才在那new Map,汗颜 Map<Character,Integer> mm = new HashMap ...