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 的朋友.所谓的朋友圈,是指所有朋友的 ...
随机推荐
- 手写vue observe数据劫持
实现代码: class Vue { constructor(options) { //缓存参数 this.$options = options; //需要监听的数据 this.$data = opti ...
- Win10删除桌面上的回收站、计算机、网络等图标
解决方案: 桌面上鼠标右键,选择个性化 个性化窗口左边侧栏选择主题 移动至最下方点击"桌面图标设置"即可看到系统中的五个桌面图标
- Spring Data JPA使用findAllOrderBy时踩的坑
Spring Data JPA使用findAllOrderBy时踩的坑 按照以往的编程经验,我的写法是这样的: List<ActivityEntity> findAllOrderByWis ...
- Codeforces Round #609 (Div. 2)前五题题解
Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...
- java动态代理、Proxy与InvocationHandler
看了好多关于代理的文章,理解和整理一下. 1.代理的基本构成 抽象角色:声明真实对象和代理对象的共同接口,这样可在任何使用真实对象的地方都可以使用代理对象. 代理角色:代理对象内部含有真实对象的引用, ...
- SEATA 分布式事务入门DEMO
Simple Extensible Autonomous Transacation Architecture,seata是简单的.可扩展.自主性高的分布式架构 SEATA Server Configu ...
- 切蛋糕(贪心 or 优先队列)
链接:https://www.nowcoder.com/acm/contest/80/D来源:牛客网 最可爱的applese生日啦,他准备了许多个质量不同的蛋糕,想请一些同学来参加他的派对为他庆生,为 ...
- mysql高级复习
MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.可以得到索引的本质:索引是数据结构,可以简单理解为“排好序的快速查找数据结构”. 数据本身之外,数据库还维护着一 ...
- Docker + node(koa) + nginx + mysql 线上环境部署
在上一篇 Docker + node(koa) + nginx + mysql 开发环境搭建,我们进行了本地开发环境搭建 现在我们就来开始线上环境部署 如果本地环境搭建没有什么问题,那么线上部署的配置 ...
- LeetCode 第三题--无重复字符的最长子串
1. 题目 2.题目分析与思路 3.思路 1. 题目 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3 ...