public class Solution {
private void dfs(int[,] M, int[] visited, int i)
{
for (int j = ; j < M.GetLength(); j++)
{
if (M[i, j] == && visited[j] == )
{
visited[j] = ;
dfs(M, visited, j);
}
}
}
public int FindCircleNum(int[,] M)
{
int[] visited = new int[M.GetLength()];
int count = ;
for (int i = ; i < M.GetLength(); i++)
{
if (visited[i] == )
{
dfs(M, visited, i);
count++;
}
}
return count;
}
}

这是第一种方法,主要是使用dfs来实现。

下面是第二种方法,是同学用python写的,我翻译成了C#,这种思路是union find。

public class Solution {
public int FindCircleNum(int[,] M)
{
var N = M.GetLength();
var groups = N;
var leads = new int[N];
for (var i = ; i < N; i++)
{
leads[i] = i;
} for (var i = ; i < N; i++)
{
for (var j = i + ; j < N; j++)
{
if (M[i, j] == )
{
var lead1 = find(i, leads);
var lead2 = find(j, leads);
if (lead1 != lead2)
{
leads[lead1] = lead2;
groups--;
}
}
}
}
return groups;
} private int find(int x, int[] parents)
{
if (parents[x] == x)
{
return x;
}
else
{
return find(parents[x], parents);
}
}
}

从耗时角度来看,第一种效率略高一些。

https://leetcode.com/problems/friend-circles/#/description

leetcode547的更多相关文章

  1. [Swift]LeetCode547. 朋友圈 | Friend Circles

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  2. Leetcode547: Friend Circles 朋友圈问题

    问题描述 在一个班级里有N个同学, 有些同学是朋友,有些不是.他们之间的友谊是可以传递的比如A和B是朋友,B和C是朋友,那么A和C也是朋友.我们定义 friend circle为由直接或者间接都是朋友 ...

  3. Leetcode547 朋友圈解题报告 (DFS

    题目描述: 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋 ...

  4. [Swift]经典解题思路:联合查找Union Find

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. 并查集算法Union-Find的思想、实现以及应用

    并查集算法,也叫Union-Find算法,主要用于解决图论中的动态连通性问题. Union-Find算法类 这里直接给出并查集算法类UnionFind.class,如下: /** * Union-Fi ...

随机推荐

  1. android 获取 图片或视频略缩图

    /** * 根据指定的图像路径和大小来获取缩略图 此方法有两点好处: 1. * 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度, * 第二次读取的bitmap是根 ...

  2. scala学习笔记(9): 语法续

    1 不定长参数 def sum(args: Int*) = { var result = 0 for ( arg <- args) result += arg result } 2 数组初始化 ...

  3. Why I am not afraid of AI (TBC)

    Freud! Yes, according to Freud's theory, most human activities are driven by libido (or aim-inhibite ...

  4. HDU - 5942 :Just a Math Problem (莫比乌斯)

    题意:略. 思路:问题转化为1到N,他们的满足mu[d]!=0的因子d个数.  即1到N的因子的莫比乌斯系数平方和. (经验:累加符号是累加的个数,我们把常数提到前面,然后用杜教筛累加个数即可. ht ...

  5. Luogu3576 POI2014 MRO-Ant colony 【树形DP】*

    Luogu3576 POI2014 MRO-Ant colony The ants are scavenging an abandoned ant hill in search of food. Th ...

  6. BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*

    BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...

  7. Oracle中Inner join和Where的区别

    1 .Where子句中使用的连接语句,在数据库语言中,被称为隐性连接.Inner join--on子句产生的连接称为显性连接.(其他Join参数也是显性连接)Where 和Inner join产生的连 ...

  8. [CF895E]Eyes Closed

    luogu description 一个序列\(a_i\),支持一下两种操作. \(1\ \ l_1\ \ r_1\ \ l_2\ \ r_2\): 随机交换区间\([l_1,r_1]\)和\([l_ ...

  9. JavaScript中的函数(一)

    javaScript中的函数实际上是对象,每一个函数都是Function类型的实例,和其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也就是一个指向函数对象的指针,也就是函数对象的一个 ...

  10. Android 使用adb查看和修改电池信息

    1.获取电池信息 $ adb shell dumpsys battery $ adb shell dumpsys battery Current Battery Service state: AC p ...