547. Friend Circles 求间接朋友形成的朋友圈数量
[抄题]:
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.
Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.
Example 1:
Input:
[[1,1,0],
[1,1,0],
[0,0,1]]
Output: 2
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
The 2nd student himself is in a friend circle. So return 2.
Example 2:
Input:
[[1,1,0],
[1,1,1],
[0,1,1]]
Output: 1
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends,
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
忘记uf怎么写了:写个find,然后初始化岛屿数量为n,再调用find来减少。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
root1和root0不相等的时候,直接把root0的全部都迁移到root1下面,而不是只迁移root[j]的
if (root0 != root1) {
roots[root1] = root0;
count--;
}
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
find里面是while循环,毕竟要一直find,保存所有路径
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int findCircleNum(int[][] M) {
//corner case
if (M == null || M.length == 0) return 0;
//initialization: count = n, each id = id
int m = M.length;
int count = m;
int[] roots = new int[m];
for (int i = 0; i < m; i++) roots[i] = i;
//for loop and union find
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
//if there is an edge, do union find
if (M[i][j] == 1) {
int root0 = find (roots, i);
int root1 = find (roots, j);
if (root0 != root1) {
roots[root1] = root0;
count--;
}
}
}
}
//return count
return count;
}
public int find (int[] roots, int id) {
while (id != roots[id]) {
id = roots[roots[id]];
}
return id;
}
}
547. Friend Circles 求间接朋友形成的朋友圈数量的更多相关文章
- LeetCode 547. Friend Circles 朋友圈(C++/Java)
题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...
- android 微信朋友分享,朋友圈分享
android 微信朋友分享,朋友圈分享 包名必须写成 com.weixin WXEntryActivity package com.weixin.wxapi; import android.app ...
- HihoCoder1084: 扩展KMP(二分+hash,求T串中S串的数量,可以失配一定次数)
时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 你知道KMP吗?它是用于判断一个字符串是否是另一个字符串的子串的算法.今天我们想去扩展它. 在信息理论中,在两个相同长度的字 ...
- [LeetCode] 547. Friend Circles 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- 547 Friend Circles 朋友圈
班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...
- [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- 547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- 【LeetCode】547. Friend Circles 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LightOj1366 - Pair of Touching Circles(求矩形内圆的对数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1366 题意:一个H*W的矩形,现在要放入两个外切的圆,问能放多少对这样的圆,其中圆心和 ...
随机推荐
- 监控端口是否开放,端口未开放关闭虚拟ip,端口开放启动虚拟IP
#!/bin/bash#该脚本监控本机的一个端口,当端口异常时,停止lvs的本地ip直到恢复.该脚本依托于lvs.sh启动脚本#目前只支持监控1个vip #定义常用变量#配置检查的ip以及端口chec ...
- jQuery 点击后退(返回)执行函数
<html> <head> <meta charset="UTF-8"> <meta name="viewport" ...
- spotlight工具监控oracle
spotlight工具版本Version: 5.0.1.1022 安装步骤 安装完成 安装之后,桌面上会生成如下图标 双机此图标,输入License 输入激活码 点击close,再次查看 建立连接,监 ...
- DokuWiki 插件使用
本身DokuWiki提供了一些公共,但是不是很强大,所以可以安装一些插件. 安装方法可以参考:https://www.dokuwiki.org/extensions 下面列举出我觉得好用的一些插件: ...
- ActiveMQ-5.15.2下载和启动(windows)
一.下载和部署 我的ActiveMQ版本是 5.15.2,参照别人家的博客,下载和启动照样成功.别人家的博客地址: http://blog.csdn.net/clj198606061111/artic ...
- Python微信
""" Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Dat ...
- Scrapy实战篇(一)之爬取链家网成交房源数据(上)
今天,我们就以链家网南京地区为例,来学习爬取链家网的成交房源数据. 这里推荐使用火狐浏览器,并且安装firebug和firepath两款插件,你会发现,这两款插件会给我们后续的数据提取带来很大的方便. ...
- 用EXCEL做快速傅立葉轉換_FFT in Excel
转载来自:http://yufan-fansbook.blogspot.tw/2013/09/excel-fft-fast-fourier-transform02.html [Excel]-用EXCE ...
- minicom 抓取log
使用minicom也有很长时间了,只用minicom抓过uart log,但是从来没有去保存过这个log,也不知道有这个功能.后来在超级终端中发现有这个功能(传送->捕获文字),想想minico ...
- docker 批量删除
杀死所有正在运行的容器docker kill $(docker ps -a -q) 删除所有已经停止的容器docker rm $(docker ps -a -q) 删除所有未打 dangling 标签 ...