Friend Circles(dfs)——LeetCode进阶路
原题链接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.
思路分析
遍历小伙伴的朋友圈,并进行标记,依次遍历为标记过的小伙伴,并对遍历的环的个数计数。
其实是一道经典的并查集,但是想到蓝桥杯dfs比较多,笔者依然坚强的用了dfs……
贴一下大神的并查集博客,当年的入门利器,爱看武林外传的阿猿不会太差~
并查集入门篇https://www.cnblogs.com/xzxl/p/7226557.html
并查集进阶篇 http://www.cnblogs.com/xzxl/p/7341536.html
AC解
class Solution {
public int findCircleNum(int[][] M) {
if(M == null || M.length == 0 || M[0].length == 0)
{
return 0;
}
boolean[] flag = new boolean[M.length];
int result = 0;
for(int i=0;i<M.length;i++)
{
if(!flag[i])
{
result ++;
dfs(M,i,flag);
}
}
return result;
}
public void dfs(int[][] M,int i,boolean[] flag)
{
flag[i] = true;
for(int j=0;j<M.length;j++)
{
if(M[i][j] == 1 && !flag[j])
{
dfs(M,j,flag);
}
}
}
}
Friend Circles(dfs)——LeetCode进阶路的更多相关文章
- DFS leetcode
把字符串转换成整数 class Solution { public: int StrToInt(string str) { int n = str.size(), s = 1; long long r ...
- leetcode 39 dfs leetcode 40 dfs
leetcode 39 先排序,然后dfs 注意先整全局变量可以减少空间利用 class Solution { vector<vector<int>>ret; vector&l ...
- 阿里巴巴 web前端性能优化进阶路
Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...
- DFS - leetcode [深度优先遍历]
最短路径=>BFS 所有路径=>DFS 126. Word Ladder II BFS+DFS: BFS找出下一个有效的word进队 并记录step 更新两个变量:unordered ...
- (二叉树 递归 DFS) leetcode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- (BFS/DFS) leetcode 200. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 回溯法和DFS leetcode Combination Sum
代码: 个人浅薄的认为DFS就是回溯法中的一种,一般想到用DFS我们脑中一般都有一颗解法树,然后去按照深度优先搜索去寻找解.而分支界限法则不算是回溯,无论其是采用队列形式的还是优先队列形式的分支界限法 ...
- LeetCode.874-走路机器人模拟(Walking Robot Simulation)
这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...
随机推荐
- 八米云-ESXi虚拟机安装教程
疑难解答加微信机器人,给它发:进群,会拉你进入八米交流群 机器人微信号:bamibot 简洁版教程访问:https://bbs.8miyun.cn 准备阶段: 八米云启动U盘(制作方法可参看<制 ...
- Linux - Centos操作系统iso文件下载
CENTOS VERSION DOWNLOAD LINK CentOS 8.5(2111) Download CentOS 8.4(2105) Download CentOS 8.3(2011) Do ...
- php禁止跨域调用api(来自文心快码)
在PHP中,禁止跨域调用API通常涉及到设置正确的HTTP响应头,以告知浏览器不允许来自不同源的请求.跨域资源共享(CORS)是一个W3C标准,它允许服务器放宽同源策略(SOP),从而允许某些跨站请求 ...
- C# 中的“相等判断”
C# 中的"相等判断" C# 中判断相等的方式很多,例如: 双等号 == 实例的 Equals() 方法 Object.Equals() 静态方法 Object.Refe ...
- Go 1.20更新了那些内容
PGO的引入 Go 1.20 发布了配置文件引导优化(PGO)的预览版,使编译器能够根据运行时配置文件信息,执行应用程序和工作负载的特定性优化.提供要构建的配置文件,使编译器能够将应用程序的速度提高大 ...
- Mac 安装php Swoole扩展出现 Enable openssl support, require openssl library 或者fatal error: 'openssl/ssl.h' file not found
Mac 安装php Swoole扩展时出现 Enable openssl support, require openssl library 或者fatal error: 'openssl/ssl.h' ...
- Oracle使用Impdp导入dmp文件的详细过程
这一天为了导入这个Oracle的dmp文件,简直就是血泪史,因本人对Oracle并不是很会,随意踩了很多小白会踩的坑,因此特意记录一下过程,防备下次的使用. 1.首先将你需要的dmp文件准备好,将其放 ...
- pagehelper的失效问题
pagehelper是常用的分页插件,代码中常用到,使用简便且对代码侵入性较小,很多人都喜欢使用.不过有时会遇到分页失败问题,输出结果没有分页,日志输出sql语句没有分页关键字及分页参数,目测是pag ...
- Noise——随机之美
本篇博文介绍图形学中噪音生成的一般方法. Noise可以干什么? 不规则表面生成 有机体模拟 流体烟雾模拟 甚至是使用noise对灯光强度,位置做扰动: 只有我们想象不到的,没有noise不能涉猎的! ...
- [源码系列:手写spring] AOP第一节:切点表达式
在本专栏之前的文章中已经带大家熟悉了Spirng中核心概念IOC的原理以及手写了核心代码,接下来将继续介绍Spring中另一核心概念AOP. AOP即切面编程是Spring框架中的一个 ...