LeetCode 547. Friend Circles 朋友圈(C++/Java)
题目:
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:
- N is in range [1,200].
- M[i][i] = 1 for all students.
- 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)的更多相关文章
- [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朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- 547 Friend Circles 朋友圈
班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...
- [LeetCode] Friend Circles 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- Leetcode547: Friend Circles 朋友圈问题
问题描述 在一个班级里有N个同学, 有些同学是朋友,有些不是.他们之间的友谊是可以传递的比如A和B是朋友,B和C是朋友,那么A和C也是朋友.我们定义 friend circle为由直接或者间接都是朋友 ...
- Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)
Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递 ...
- Java实现 LeetCode 547 朋友圈(并查集?)
547. 朋友圈 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指 ...
- [LeetCode]547. 朋友圈(DFS)
题目 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集 ...
- LeetCode 547 朋友圈
题目: 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的 ...
随机推荐
- ECMAScript新语法、特性总结
前言 从2015年的ES6开始,JavaScript的语言标准每年都在更新,其中尤其以ES6的力度之大,到现在ES10已经发布,这里总结一下新语法. 参考:阮一峰 ECMAScript 6 教程 .E ...
- 1030 完美数列 (25 分)C、Java、python
题目描述 给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列. 现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一个完 ...
- windows下使用cmd命令杀死进程
tasklist 使用以上命令显示当前进程,及其PID等,如图所示 找到我要删除的进程的pid(好像有点费劲?) 出现以下提示 原因:没有管理员权限,使用管理员模式打开 在Windows菜单栏中找到命 ...
- JS中常见的几种继承方法
1.原型链继承 // 1.原型链继承 /* 缺点:所有属性被共享,而且不能传递参数 */ function Person(name,age){ this.name = name this.age = ...
- 【转】在NetBeans上搭建Android SDK环境
本文将介绍在NetBeans 6.8上搭建Android SDK环境,目前Android在Netbeans上进行开发需要借助nbandroid的平台插件. 我们刚刚介绍过<MyEclipse上搭 ...
- es6中的面向对象写法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 深度学习论文翻译解析(七):Support Vector Method for Novelty Detection
论文标题:Support Vector Method for Novelty Detection 论文作者:Bernhard Scholkopf, Robert Williamson, Alex Sm ...
- 机器学习新手必看:Jupyter Notebook入门指南
参考网址:https://blog.csdn.net/guleileo/article/details/80490921
- Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解
微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 哎呦喂,按照以往的惯例今天周六我的安排应该是待在家学学猫叫啥的.但是今年这种日子就可能一去不复返了,没法办法啊.前 ...
- LCA - 倍增法去求第几个节点
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...