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), write a function to find the number of connected components in an undirected graph.
Example 1:
Input:n = 5andedges = [[0, 1], [1, 2], [3, 4]]0 3
| |
1 --- 2 4 Output: 2
Example 2:
Input:n = 5andedges = [[0, 1], [1, 2], [2, 3], [3, 4]]0 4
| |
1 --- 2 --- 3 Output: 1
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道线段怎么加
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
线段也是由点构成的,分成两个点来加
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
每次更新的都是roots数组,把新的root指定给roots数组中的元素
//merge if neccessary
if (root1 != root0) {
roots[root1] = root0;
count--;
}
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(1) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:递归
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int countComponents(int n, int[][] edges) {
//use union find
//ini
int count = n;
int[] roots = new int[n];
//cc
if (n == 0 || edges == null) return 0;
//initialization the roots as themselves
for (int i = 0; i < n; i++)
roots[i] = i;
//add every edge
for (int[] edge : edges) {
int root0 = find(edge[0], roots);
int root1 = find(edge[1], roots);
//merge if neccessary
if (root1 != root0) {
roots[root1] = root0;
count--;
}
}
//return
return count;
}
public int find(int id, int[] roots) {
while (id != roots[id])
id = roots[roots[id]];
return id;
}
}
323. Number of Connected Components in an Undirected Graph按照线段添加的并查集的更多相关文章
- 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 ...
- 323. Number of Connected Components in an Undirected Graph
算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...
- [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), ...
随机推荐
- ios系统降级
1.使用PP助手/iTunes备份好文件资料,以防重要信息丢失: 2.设备连接iTunes,按住Shift键之后点击“恢复iPhone”,选择已下载好的iOS8.4.1固件,等待更新完成即可. 注意要 ...
- php session目录找不到的错误 Error session_start(): open(/var/lib/php/session error
问题来源 今天安装一个应用,发现提示 Error session_start(): open(/var/lib/php/session error,估计是找不到写不了啥啥啥. 于是我就去该路径下去看看 ...
- Python流程控制-逻辑运算-if...else语句
摘录自:http://www.runoob.com/python/python-if-statement.html Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执 ...
- BZOJ5091: [Lydsy1711月赛]摘苹果(简单概率)
5091: [Lydsy1711月赛]摘苹果 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 214 Solved: 163[Submit][Statu ...
- Mac触摸板没有弹性了
关机后,同时按启动键,空格键左边的option,command键还有p和r,听到开机声音响四声后再松开.一定要同时按!然后触摸板就可以用了. (转自知乎)
- 关于const 和指针
这个很久之前就很困扰的问题,现在再理一下: 1,指向const对象的指针 >C++强制要求指向const对象的指针也必须具有const特性!!!也就是不能把一个const对象的地址赋给一个非co ...
- 重新学习Spring之核心IOC容器的底层原理
一:IOC容器的定义 控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般 ...
- wpf 客户端【JDAgent桌面助手】业余开发的终于完工了。。晒晒截图
目录区域: 业余开发的wpf 客户端终于完工了..晒晒截图 wpf 客户端[JDAgent桌面助手]开发详解-开篇 wpf 客户端[JDAgent桌面助手]详解(一)主窗口 圆形菜单... wpf 客 ...
- python和C语言互相调用的几种方式
? 1 2 3 4 5 6 7 8 9 版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/ ...
- [您有新的未分配科技点]可,可,可持久化!?------0-1Trie和可持久化Trie普及版讲解
这一次,我们来了解普通Trie树的变种:0-1Trie以及在其基础上产生的可持久化Trie(其实,普通的Trie也可以可持久化,只是不太常见) 先简单介绍一下0-1Trie:一个0-1Trie节点只有 ...