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), ...
随机推荐
- Nginx(Windows)
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器. 反向代理方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将 ...
- BZOJ4012: [HNOI2015]开店【动态点分治】
Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...
- 在服务器上运行db:seed数据填充时,出错的问题解决
在服务器上运行db:seed数据填充时,出错的问题解决 运行composer dump-autoload
- 如何解决前后端token过期问题
问题描述: 首先后端生成的token是有时限的,在一段时间后不管前端用户是否进行了访问后端的操作,后端的token都会过期,在拦截器阶段就会返回错误的请求:token过期,从而拿不到想要的请求数据. ...
- Spring Bean单例与线程安全
一.Spring单例模式及线程安全 Spring框架中的Bean,或者说组件,获取实例的时候都是默认单例模式,这是在多线程开发的时候需要尤其注意的地方. 单例模式的意思是只有一个实例,例如在Sprin ...
- CentOS7 tomcat systemctl脚本
1,centos7 使用 systemctl 替换了 service命令 参考:redhat文档: https://access.redhat.com/documentation/en-US/Red_ ...
- Window 查看端口进程使用
1. 查看所有端口占用 netstat -ano 2. 查看指定端口占用 netstat -ano | findstar "8080" 其中11152 对应为进程号 3. 查看进程 ...
- jQuery ajax 当async为false时解决同步操作失败的问题
jQuery的ajax,当async为false时,同步操作失败.解决方案,jqueryasync 最近做项目遇到jQuery的ajax,当async为false时,同步操作失败的问题,上网搜索下,得 ...
- 由浅入深了解Thrift(1,2,3)
由浅入深了解Thrift(一)——Thrift介绍与用法 由浅入深了解Thrift(二)——Thrift工作原理 由浅入深了解Thrift(三)——Thrift server端的几种工作模式分析
- python博客
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000