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)开始并朝北.机器人可以接收三 ...
随机推荐
- Markdown 编写技巧汇总(二)
继续上篇汇总 附-上篇汇总,接着做更加高级一点的应用技巧. [1]列表与引言嵌套 两者嵌套,如: > 我是一行文本 > 1. 文本1 > 2. 文本2 > 1. 文本 > ...
- 大数据之路Week10_day05 (JavaAPI 操作Redis 第一阶段)
刚开始学习JavaAPI的时候,主要是对redis中的字符串,字节位图,列表,集合,有序集合进行操作,并能够完成简单的需求. package com.wyh.redis; import org.jun ...
- 怎么解决DB读写分离,导致数据不一致问题?
前言 在互联网中大型项目中,读写分离应该是我们小伙伴经常听说的,这个主要解决大流量请求时,提高系统的吞吐量.因为绝大部分互联网产品都是读多写少,大部分都是读请求,很小部分是写请求. 上图: 1)一个主 ...
- ORACLE SQL中执行先后次序的问题
分享一个经验 需求:Oracle中,根据COST优先级取最优先的一条记录脚本: select ... from ... where ... and rownum=1 order by cost 实际不 ...
- docker搭建本地仓库
环境准备: 服务器:9.134.130.35 私有仓库服务器,运行registry容器 客户端:9.208.244.175 测试客户端,用于上传.下载镜像文件 测试搭建本地仓库 mkdir /dock ...
- “决策-寻找过程”的黄金秘密工具,1/e 法则之应用(尤其日常生活中的应用)
https://www.ccgxk.com/magicword/327.html 目录 引言 著名的 1/e 法则内容和解释 应用到生活中的 1/e 法则是什么样? 相亲案例 看书.看电影案例 生活质 ...
- Effective Java理解笔记系列-第2条-何时考虑用构建器?
为什么写这系列博客? 在阅读<Effective Java>这本书时,我发现有许多地方需要仔细认真地慢慢阅读并且在必要时查阅相关资料才能彻底搞懂,相信有些读者在阅读此书时也有类似感受:同时 ...
- 【JVM之内存与垃圾回收篇】垃圾回收概述
垃圾回收概述 概念 这次我们主要关注的是黄色部分,内存的分配与回收 垃圾收集 垃圾收集,不是 Java 语言的伴生产物.早在 1960 年,第一门开始使用内存动态分配和垃圾收集技术的 Lisp 语言诞 ...
- Java的IO模型、Netty原理详解
1.什么是IO 虽然作为Java开发程序员,很多都听过IO.NIO这些,但是很多人都没深入去了解这些内容. Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的 ...
- 《Python基础教程》第三版语录
对程序的结构(如需要哪些类和函数)有一定的想法后,建议你实现一个功能可能极其有限的简单版本. 当你有了可运行的程序后,将发现接下来的工作容易得多.你可添加新功能,修改不喜欢的方面,等等.这样你才能够真 ...