[抄题]:

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 = 5 and edges = [[0, 1], [1, 2], [3, 4]]

     0          3
| |
1 --- 2 4 Output: 2

Example 2:

Input: n = 5 and edges = [[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按照线段添加的并查集的更多相关文章

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

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

  2. 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), ...

  3. [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), ...

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

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

  5. 323. Number of Connected Components in an Undirected Graph

    算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...

  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. Linux:自动获取静态IP地址,清空iptable,修改selinux脚本

    自动获取静态IP地址,清空iptable,修改selinux脚本 环境:VMware 平台:centos6.8全新 功能: 1)应用ifconfig -a,route -n,cat /etc/reso ...

  2. react中父组件调用子组件的方法

    1.直接使用ref进行获取 import React, {Component} from 'react'; export default class Parent extends Component ...

  3. 如何在未越狱的ios系统安装ipa文件

    首先我们先下载一个PP助手正版在电脑上 用iphone打开http://z.25pp.com/?from=bdpz,根据网页上的教程,我们安装好PP助手正版(注意是正版!!) 将手机连接电脑,在电脑上 ...

  4. Java SE 8 流库(一)

    1. 流的作用 通过使用流,说明想要完成什么任务,而不是说明如何去实现它,将操作的调度留给具体实现去解决: 实例:假如我们想要计算某个属性的平均值,那么我们就可以指定数据源和属性,然后,流库就可以对计 ...

  5. Ubuntu系统配置apt-get软件更新源

    从别人那摘录的Ubuntu源,测的很好用 # 电子科技大学 (教育网) deb http://ubuntu.uestc.edu.cn/ubuntu/ oneiric main restricted u ...

  6. 对ashx请求用Gzip,Deflated压缩

    //GZIP压缩 //查看请求头部 string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToStr ...

  7. Mplayer1.0rc2移植到am335x开发板

    因项目需要媒体播放器,所以准备使用QT+Mplayer来做,但遇到了屏幕闪烁的问题,无法满足需求. 1.参考<mplayer 移植到 arm 心得> ,http://blog.csdn.n ...

  8. CentOS7 防火墙配置-详解

    CentOS 7 防火墙配置 1.防火墙的简述 防火墙对服务器起到一定的保护作用,所以了解一些相关的操作是很有必要的. 在CentOS 7.x中,有了一种新的防火墙策略叫FireWall , 在6.x ...

  9. Linux内核 TCP/IP参数调优

    http://www.360doc.com/content/14/0606/16/3300331_384326124.shtml

  10. Python 字串处理

    #!/usr/bin/python #-*- coding:utf-8 –*- import os import sys import re import shutil import xlrd imp ...