题目地址:Friend Circles - LeetCode


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.


这道题目我有印象是一家公司的笔试算法题,意思是无向图找分成几个部分,几个环。

当时的Python解法如下:

class Solution:def findCircleNum(self, M: List[List[int]]) -> int:num = len(M)s = {i: set() for i in range(0, num)}for i in range(0, num):for j in range(0, num):if M[i][j] == 1:s[i].add(j)stack = set([0])passed = set()result = 1while stack != set():temp=stack.pop()passed.add(temp)for i in s[temp]:if i not in passed:stack.add(i)if stack==set():if len(passed)==num:breakelse:result+=1stack.add(list(set(s)-passed)[0])return result

上面的解法肯定不是最优的,但已经可以过所有样例了。
具体的思路是从一个人开始,进行BFS遍历,遍历完后找没遍历过的人开始BFS,一直到结束。
然后我看了别人的解答,发现并没有比我的做法好很多。。。。

LeetCode 547. Friend Circles--Python解法--笔试算法题相关推荐

  1. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  2. LeetCode 2. Add Two Numbers--C++,Python解法--面试算法题

    题目地址:Add Two Numbers - LeetCode You are given two non-empty linked lists representing two non-negati ...

  3. Re: 一著名软件公司的java笔试算法题!

    主 题: Re: 一著名软件公司的java笔试算法题! 作 者:   szlhj () 等 级:   信 誉 值:   95 所属论坛:   Java J2SE / 基础类 问题点数:   20 回复 ...

  4. 【实习笔试面试题】2013网易互联网实习笔试算法题-找出最大连续自然数个数

    找出最大连续自然数个数 搜集者:江南烟雨 E-Mail:xiajunhust@gmail.com 本题为网易互联网暑期实习生笔试算法题. 凭记忆记录下来的题目,如违反网易版权请邮件联系,本人会删除. ...

  5. 秋招笔试算法题——电容充电

    秋招笔试算法题--电容充电 牛客网<笔试真题精选2019> 字节跳动2018秋招笔试题4 [题目描述]有一台用电容组成的计算器,其中每个电容组件都有一个最大容量值(正整数). 对于单个电容 ...

  6. LeetCode 92. Reverse Linked List II--Python 解法--反转部分链表--笔试算法题

    题目地址:Reverse Linked List II - LeetCode Reverse a linked list from position m to n. Do it in one-pass ...

  7. LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法

    题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked lists representing two non-neg ...

  8. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  9. 【Leetcode】5 longestPalindrome python解法

    leetcode第5题 longestPalindrome 最长回文子串 python解法 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 输入: " ...

最新文章

  1. GEMM性能提升200倍,AutoKernel算子优化工具正式开源
  2. Windows 技术篇 - 电脑秒速关机设置方法,注册表修改3个缓冲等待时间
  3. SAP Spartacus org unit页面的三种focus border及细节讨论
  4. linux卸载minicoda2,MiniConda2下载 MiniConda python 2.7 v4.3.30.2 Linux 64位 官方免费版(附安装步骤) 下载-脚本之家...
  5. 双十一变身大型奥数竞赛现场?数学不好的你请转场
  6. MAC 设置$PATH 关闭terminal后就失效 解决方案
  7. java查询数据库数据放到Excel下载
  8. WayOs扩展WAN口工具1.4隆重发布,同时发布BCM内置三天智能重启超级终端调试图...
  9. 人工智能在塑造未来移动应用开发中的作用
  10. Log4net PatternLayout 参数
  11. 设计模式学习笔记(十四:单件模式)
  12. uniapp使用阿里icon详细步骤
  13. python向excel删除数据_python 操作 excel 系列之:数据清洗
  14. python爬虫爬取圆通快递信息
  15. 帝国CMS文章内容超级伪静态 去除栏目id 以自定义英文名称和ID组成
  16. 回顾 2021,展望 2022
  17. 软件测试与质量保障之间的关系
  18. 自动化运维为什么是必须的?
  19. 说一下自动化测试的优缺点
  20. 跨平台应用开发进阶(四十五)uni-app集成企微客服实战

热门文章

  1. 第十八课.支持向量机
  2. android布局共享,布局共享(如所有ACTIVITY拥有相同的布局部分,比如ACTIONBAR,在BASEACTIVITY中写入布局)...
  3. java二个整数相减_Java-消息框显示两整数加减乘除
  4. QIIME 2教程. 15样品分类和回归q2-sample-classifier(2021.2)
  5. QIIME1.9.1-1虚拟机安装配置及挂载外部目录
  6. 肠道微生物的研究不复杂,不信看这篇Science
  7. Python使用matplotlib可视化柱状图、坐标轴标签的符号(-)显示为了方框□□、设置rcParams参数配置解决
  8. pandas使用iloc函数基于dataframe数据列的索引抽取单列或者多列数据、其中多列索引需要嵌入在列表方括号[]中、或使用:符号形成起始和终止范围索引
  9. python使用FPDF包将多个图像文件写入pdf文件实战
  10. python使用imbalanced-learn的SMOTEN方法进行上采样处理数据不平衡问题