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)开始并朝北.机器人可以接收三 ...
随机推荐
- 使用mybatis-plus转换枚举值
1. 使用mybatis-plus转换枚举值 枚举值转换方式有很多,有以下方式: 后端写一个通用方法,只要前端传枚举类型,后端返回相应的枚举值前端去匹配 优点:能够实时保持数据一致性 缺点:如果有大量 ...
- PHP中处理html相关函数集锦
1.html_entity_decode() 函数把 HTML 实体转换为字符. Html_entity_decode() 是 htmlentities() 的反函数. 例子: <?Php $s ...
- [tldr] 使用ip.sb检查自己所在局域网的公网IP
使用ip a等一些命令行工具可以帮助我们检查自己的内网IP,但是,如何获取自己的在公网下的IP(即当前所在的局域网被分配的公网IP) 如果使用爬虫,这个IP也是很重要的.BAN IP就是这个IP ht ...
- go context 子Goroutine超时控制
context使用 Go语言第一形参通常都为context.Context类型,1. 传递上下文 2. 控制子Goroutine超时退出 3. 控制子Goroutine定时退出 package mai ...
- 使用Docker部署服务
一.Docker概念 1.操作系统层面的虚拟化技术 2.隔离的进程独立于宿主和其它的隔离的进程 - 容器 3.GO语言开发 4.特点:高效的利用系统资源:快速的启动时间:一致的运行环境:持续交付和部署 ...
- Delphi WebBrowser内核版本修改D7
private { Private declarations } public { Public declarations } function WriteAppNameToReg:Boolean; ...
- 微服务架构的守护者:Redisson 分布式锁与看门狗机制实战指南
1. 分布式锁简介 1.1 什么是分布式锁 在单机应用中,可以使用 Java 内置的锁机制(如 synchronized.ReentrantLock 等)来实现线程间的同步.但在分布式环境下,由于应用 ...
- Python进阶知识:多进程/多线程/装饰器
本文写作于2025.3.20,恰好作者正好在外面实习,于此同时在实际工作中遇到这些知识点,因此就进行一个简短汇总方便后续回顾,内容同步更新(显示问题可以直接看):https://www.big-yel ...
- 【SpringCloud】各种组件的更新情况
关于Cloud各种组件的停更/升级/替换 由停更引发的"升级惨案" 停更不停用 被动修复bugs 不再接受合并请求 不再发布新版本 以前 now2020 服务注册中心 Eureka ...
- SQLite 下载与安装
安装 SQLite 官网 就2个文件,下载解压到一起即可 这里不是安装包的形式,下载后是下面这2个文件: 解压到同一目录下即可,如图: "安装"完成后,记得添加系统环境变量,以便命 ...