Leetcode547: Friend Circles 朋友圈问题
问题描述
在一个班级里有N个同学, 有些同学是朋友,有些不是。他们之间的友谊是可以传递的比如A和B是朋友,B和C是朋友,那么A和C也是朋友。我们定义 friend circle为由直接或者间接都是朋友组成的group.
给定N*N 数组 M 代表同学之间的关系. 如果M[i][j] = 1, 那么i 和 j 同学是朋友,现在我们需要输出friend circles 的数量
Example 1:
Input:
[[1,1,0],
[1,1,0],
[0,0,1]]
Output: 2
解释: 0和1号同学是朋友,所以他们是一个friend cricle,2号同学自己是个friend circle,所有返回2。
Example 2:
Input:
[[1,1,0],
[1,1,1],
[0,1,1]]
Output: 1
解释: 0号同学和1号同学是朋友,1号和2号同学是朋友,那么0,1,2都是朋友,他们组成一个friend circle,所以结果返回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数组看做是一个graph, M[i][j] = 1 表示节点i 和 j 相连,那么寻找fridend circle的问题,转化为图遍历的问题,求fiend circle的个数实则为求连通图的个数。解决这类问题通常使用DFS或者BFS去更新一个状态数组visited(保存节点是否被访问),对每个未访问的节点调用图形遍历算法,每次调用所有相连节点的状态都会被更新为已访问,那么friend cicle的数量则为调用遍历的次数。
DFS实现
public int findCircleNum(int[][] M) {
int N = M.length;
boolean[] visited = new boolean[N];
int cnt = 0;
for(int i = 0; i < N; i++){
if(!visited[i]){
dfs(M, visited, i);
cnt++;
}
}
return cnt;
}
private void dfs(int[][] M, boolean[] visited, int i){
visited[i] = true;
for(int j = 0; j < M.length; j++){
if(M[i][j] == 1 && !visited[j]){
dfs(M, visited, j);
}
}
}
时间复杂度:O(n^2),总体上要遍历M数组的所有位置
空间复杂度:O(n)
BFS实现
public int findCircleNum(int[][] M) {
boolean[] visited = new boolean[M.length];
int count = 0;
Queue < Integer > queue = new LinkedList < > ();
for (int i = 0; i < M.length; i++) {
if (!visited[i]) {
queue.add(i);
while (!queue.isEmpty()) {
int s = queue.remove();
visited[s] = true;
for (int j = 0; j < M.length; j++) {
if (M[s][j] == 1 && !visited[j]])
queue.add(j);
}
}
count++;
}
}
return count;
}
时间复杂度:O(n^2),总体上要遍历M数组的所有位置
空间复杂度:O(n)
Leetcode547: Friend Circles 朋友圈问题的更多相关文章
- [LeetCode] 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 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- LeetCode 547. Friend Circles 朋友圈(C++/Java)
题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...
- 547 Friend Circles 朋友圈
班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...
- [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- [Swift]LeetCode547. 朋友圈 | Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)
Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递 ...
- Leetcode547 朋友圈解题报告 (DFS
题目描述: 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋 ...
- QQ空间/朋友圈类界面的搭建
类似于QQ空间的布局主要是在说说信息.点赞.回复三大部分的自适应布局上. 当我们需要搭建类似QQ空间.微信朋友圈的界面的时候,可做如下操作: 创建一个对应的model类: 创建一个对应model类的f ...
随机推荐
- day20191106
笔记: 一.#{}和${}的区别是什么 1)#{}是预编译处理,${}是字符串替换.2)Mybatis 在处理#{}时,会将 sql 中的#{}替换为?号,调用 PreparedStatement 的 ...
- 英语口语考试资料Food
新东方推荐文章:Food and Health The food we eat seems to have profound effects on our health. Although scien ...
- 第一次c语言作业。
第一次c语言作业 作业1 2.1 你对软件工程专业或者计算机科学与技术专业了解是怎样? 我认为计算机科学与技术是研究信息过程.用以表达此过程的信息结构和规则及其在信息处理系统中实现的学科.这门学科是为 ...
- .Neter所应该彻底了解的委托
本文将通过引出几个问题来,并且通过例子来剖析C#中的委托以及用法,做抛砖引玉的作用 对于委托我发现大部分人都有以下问题,或者可能在面试中遇过这样的: 委托是不是相当于C/C++的函数指针? 委托究竟是 ...
- 转:<context:component-scan>使用说明
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...
- luogu P2740 [USACO4.2]草地排水Drainage Ditches |网络流
题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...
- 【解决】image ... could not be accessed on a registry to record its digest.
[问题]image jmdiservice:1206 could not be accessed on a registry to record its digest. Each node will ...
- 2018HDU多校训练一 D Distinct Values
hiaki has an array of nn positive integers. You are told some facts about the array: for every two e ...
- 纯手工搭建K8s(单节点)
准备说明: 因为为纯手动搭建,所以针对安装时需要的一些安装包需提前下载好 cfssl_linux-amd64. cfssljson_linux-amd64. cfssl-certinfo_linux- ...
- intellij idea使用tomcat maven plugin
环境 java 1.8.0_111 tomcat tomcat-8.5.11 maven 3.2.5 intellij idea 14.0.3 命令行使用 建maven工程 mvn archetype ...