题目:

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. Head First设计模式——组合模式

    最近比较忙,有段时间没有更新设计模式的进度了.今天继续学习组合设计模式. 组合模式的例子我们继续延续上篇<Head First设计模式——迭代器模式>的菜单例子,首先声明下迭代器和组合模式 ...

  2. Java工作流引擎节点接收人设置20种规则讲解一

    关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 开发者表单  拖拽式表单 工作流系统CCBPM节点访问规则接收人规则 适配数据库: o ...

  3. bootstrap:导航分页

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  4. ASCII、UNICODE、UTF

    在计算机中,一个字节对应8位,每位可以用0或1表示,因此一个字节可以表示256种情况. ascii 美国人用了一个字节中的后7位来表达他们常用的字符,最高位一直是0,这便是ascii码. 因此asci ...

  5. cometoj 茶颜悦色|扫描线+懒惰标记

    传送门 题目描述 茶颜悦色也太好喝了!鸡尾酒在长沙的各种茶颜悦色的店铺中流连忘返.他发现长沙有炒鸡多的茶颜悦色店,走两步就能遇到一家. “方圆一公里能有十家茶颜悦色!”鸡尾酒感叹了起来. 于是他想到了 ...

  6. Thinkpad S440 I/O接口配置

    HDMI 视频接口 SS USB3.0接口 电源接口 音频接口 网络接口 没有com口可以用USB口,然后安装一个USB转com口的驱动.

  7. hdu - 4990

    Read the program below carefully then answer the question.    #pragma comment(linker, "/STACK:1 ...

  8. mysql 向字段添加数据或者删除数据

    UPDATE table SET cids = CONCAT(cids , ',12') where id=id //向字段添加数据 //因为要用逗号分隔 所以在在前面加了一个逗号 UPDATE ta ...

  9. 2018南京现场赛D 模拟退火

    题目链接:https://codeforces.com/gym/101981/attachments 给你n个城市的三维坐标,叫你求得一个坐标使这个坐标到其他城市的最大距离最小,并输出这个距离(距离不 ...

  10. 想玩转JAVA高并发,这些概念你必须懂!

    我们在找工作时,经常在招聘信息上看到有这么一条:有构建大型互联网服务及高并发等经验,你第一时间想到的是媒体常说的双十一吗?带着问题,我们一起思考技术…. 高并发高并发 它是互联网分布式系统架构设计中必 ...