题目:

https://leetcode.com/problems/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:

  1. N is in range [1,200].
  2. M[i][i] = 1 for all students.
  3. If M[i][j] = 1, then M[j][i] = 1.

分析:

有n个学生,如果a和b是朋友,b和c是朋友,那么a和c也是朋友,求出学生中的朋友圈个数(a,b,c同属一个朋友圈)

朋友之间的关系由矩阵表示,1表示是朋友关系,同时自己和自己也是朋友关系。

首先我们从第一个学生开始遍历,利用M[i][i]是否等于1来判断是否访问过这个学生,然后从这个学生开始去扫描其他的学生是否和这个学生是朋友关系,如果是的话,将他们的关系修改为非朋友,同时继续搜索下去,直到这个圈子不再有新的学生了,同时结果加1,因为这个朋友圈所有的人我们都遍历过了,然后再继续访问后面的学生,如果有M[i][i]等于1的话,意味着有新的圈子出现,继续搜索即可,处理完一个朋友圈结果记得加一。

程序:

C++

class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
if(M.size() == )
return ;
m = M.size();
int res = ;
for(int i = ; i < m; ++i){
if(M[i][i] == ){
dfs(M, i);
res++;
}
}
return res;
}
private:
void dfs(vector<vector<int>>& M, int i){
if(M[i][i] == )
return;
M[i][i] = ;
for(int k = ; k < m; ++k){
if(M[k][i] == ){
M[k][i] = M[i][k] = ;
dfs(M, k);
}
}
}
int m;
};

Java

class Solution {
public int findCircleNum(int[][] M) {
if(M.length == 0)
return 0;
m = M.length;
int res = 0;
for(int i = 0; i < m; ++i){
if(M[i][i] == 1){
dfs(M, i);
res++;
}
}
return res;
}
private void dfs(int[][] M, int i){
if(M[i][i] == 0)
return;
M[i][i] = 0;
for(int k = 0; k < m; ++k){
if(M[k][i] == 1){
M[k][i] = M[i][k] = 0;
dfs(M, k);
}
}
}
private int m;
}

LeetCode 547. Friend Circles 朋友圈(C++/Java)的更多相关文章

  1. [LeetCode] 547. Friend Circles 朋友圈

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  2. [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题

    /* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...

  3. 547 Friend Circles 朋友圈

    班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...

  4. [LeetCode] Friend Circles 朋友圈

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  5. Leetcode547: Friend Circles 朋友圈问题

    问题描述 在一个班级里有N个同学, 有些同学是朋友,有些不是.他们之间的友谊是可以传递的比如A和B是朋友,B和C是朋友,那么A和C也是朋友.我们定义 friend circle为由直接或者间接都是朋友 ...

  6. Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)

    Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递 ...

  7. Java实现 LeetCode 547 朋友圈(并查集?)

    547. 朋友圈 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指 ...

  8. [LeetCode]547. 朋友圈(DFS)

    题目 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集 ...

  9. LeetCode 547 朋友圈

    题目: 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的 ...

随机推荐

  1. CF1200E Compress Words | 字符串hash

    传送门 Examples input 1 5 I want to order pizza output 1 Iwantorderpizza input 2 5 sample please ease i ...

  2. 低副瓣阵列天线综合2 matlab HFSS

    接着继续研究阵列天线设计,得到了电流幅度分布或功率分布之后,就可以进行阵列设计或馈电网络设计了,之前利用HFSS仿真过单列的串馈天线,后面会继续复习熟悉一下,本次我找了一篇硕士论文利用威尔金森功分器来 ...

  3. Winform在控件内实现简单画笔功能

    using System.Drawing; using System.Windows.Forms; namespace ZhuoHuiSchoolroom.ZhuoHuiClass { /// < ...

  4. springboot整合apache ftpserver详细教程(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...

  5. Scala实践13

    1.隐式参数 方法可以具有隐式参数列表,由参数列表开头的implicit关键字标记.如果该参数列表中的参数没有像往常一样传递,Scala将查看它是否可以获得正确类型的隐式值,如果可以,则自动传递. S ...

  6. <a>标签的href和onclick属性【转】

    1链接的onclick 事件被先执行,其次是href属性下的动作(页面跳转,或 javascript 伪链接): 2假设链接中同时存在href 与onclick,如果想让href 属性下的动作不执行, ...

  7. Unity事件系统EventSystem简析

    相关组件和类 EventSystem 1.负责InputModule的切换(因为现在游戏大部分都只有一个StanaloneInputModule,所以切换这部分可以先不考虑). 2.负责InputMo ...

  8. Scrapy解析器xpath

    一.使用xpath 不在scrapy框架中通过response from scrapy.http import HtmlResponse HtmlResponse->TextResponse-& ...

  9. 认识一下 RabbitMQ

    分布式系统中,如何在各个应用之间高效的进行通信,是系统设计中的一个关键. 使用 消息代理(message broker) 是一个优雅的解决方案. RabbitMQ 就是一个被广泛应用的消息代理,遵循 ...

  10. 关于C#程序的单元测试

    目录 1.单元测试概念 2.单元测试的原则 3.单元测试简单示例 4.单元测试框架特性标签 5.单元测试中的断言Assert 6.单元测试中验证预期的异常 7.单元测试中针对状态的间接测试 8.单元测 ...