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.
Note:
- N is in range [1,200].
- M[i][i] = 1 for all students.
- If M[i][j] = 1, then M[j][i] = 1.
M[0][1] 表示0和1是直接好友关系
M[1][2] 表示1和2是直接好友关系 0,2是间接好友关系 他们是一个朋友圈
求有多少个朋友圈,也就是无向图的连通分量
C++:
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
int n = M.size() ;
int res = ;
vector<bool> visit(n , false) ;
for(int i = ; i < n ; i++){
if (!visit[i]){
dfs(i,M,visit) ;
res++ ;
}
}
return res ;
}
void dfs(int i , vector<vector<int>> M , vector<bool>& visit){
visit[i] = true ;
for(int j = ; j < M.size() ; j++){
if (M[i][j] == && !visit[j]){
dfs(j,M,visit) ;
}
}
}
};
547. Friend Circles的更多相关文章
- [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 求间接朋友形成的朋友圈数量
[抄题]: There are N students in a class. Some of them are friends, while some are not. Their friendshi ...
- LeetCode 547. Friend Circles 朋友圈(C++/Java)
题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...
- 【LeetCode】547. Friend Circles 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 547 Friend Circles 朋友圈
班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...
- [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode编程训练 - 合并查找(Union Find)
Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...
- UnionFind问题总结
UnionFind就是acm中常用的并查集... 并查集常用操作 另外补充一下STL常用操作 相关问题: 547. Friend Circles 纯裸题噢... class Solution { pu ...
随机推荐
- Dubbo--基于Zookeeper服务与Spring集成
Dubbo Zookeeper Spring 1.部署dubbo服务管理中心 2.搭建dubbo服务环境 2.1 pom.xml 依赖 2.2 log4j.properties 日志打印 3.api ...
- centos6.9安装crontab
yum install vixie-cron crontabs //安装 chkconfig crond on //开机自启动 service crond start //启动 然后就是执行 cron ...
- servlet生成图片验证码
package cn.itcast.servlet.session.demo3; import java.awt.Color; import java.awt.Font; import java.aw ...
- List集合三种遍历方法
List<String> list = new ArrayList<String>();list.add("aaa");list.add("bbb ...
- 如何查看当前应用包名和activity
这里提供一个简单的方法来获取package和activity: 在Android模拟器上打开微信APP,然后打开CMD,输入以下命令: adb shell 接下来在#后面继续输入以下命令: logca ...
- 在线HTTP POST/GET接口测试工具 - aTool在线工具
百度搜索标题或直接访问网址如下 网址:http://www.atool.org/httptest.php 很好用的在线http get/post 测试工具
- Oracle 数据库实例简介
回到顶部 一:Oracle 数据库实例简介 1:数据库实例的启动顺序: 使用数据库其实就是访问内存.即:数据库实例.数据库的启动是顺序是 先 nomount ----> mount --- ...
- Java Web 开发的JavaBean + Servlet + Sql Server
日期:2018.12.9 博客期:026 星期日 我知道对于每个人都需要对开发web进行了解,而我们常用的技术,也应该有所了解 /*<------------------->*/知识点: ...
- BIgnum类的程序提交
日期:2018.7.19 星期四 博客期:002 这之前赶着做一个单机游戏的修改器忘了时间,不好意思啊!今天我就把Bignum类的源代码发出来,文件的话,我不知道怎样发,待我好好研究研究这个网站哈!因 ...
- 放一点百度来的,常见的windowserror
0操作成功完成.1功能错误.2系统找不到指定的文件.3系统找不到指定的路径.4系统无法打开文件.5拒绝访问.6句柄无效.7存储控制块被损坏.8存储空间不足,无法处理此命令.9存储控制块地址无效.10环 ...