• LeetCode 547 朋友圈


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

    给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道。你必须输出所有学生中的已知的朋友圈总数。

    示例 1:

    输入: 
    [[1,1,0],
     [1,1,0],
     [0,0,1]]
    输出: 2 
    说明:已知学生0和学生1互为朋友,他们在一个朋友圈。
    第2个学生自己在一个朋友圈。所以返回2。
    

    示例 2:

    输入: 
    [[1,1,0],
     [1,1,1],
     [0,1,1]]
    输出: 1
    说明:已知学生0和学生1互为朋友,学生1和学生2互为朋友,所以学生0和学生2也是朋友,所以他们三个在一个朋友圈,返回1。
    

    注意:

    1. N 在[1,200]的范围内。
    2. 对于所有学生,有M[i][i] = 1。
    3. 如果有M[i][j] = 1,则有M[j][i] = 1。

    解析:这道题需要申清题意,矩阵为n*n的,矩阵中每个位置的值代表是不是为朋友。

    1、这道题其实可以按照最大岛屿个数的那道题来做,最大岛屿个数在求的时候 是搜索每一个值的上下左右紧邻的元素,但是这个题可以搜索同一横排和同一竖排的所有值,然后再往深处搜(或者广处搜)

    2、还有一种解法就是并查集

    1、深度优先搜索

        class Solution {
            int len;
            public int findCircleNum(int[][] m) {
                len = m.length;
                int ans = 0;
                for (int i = 0; i < len; i++) {
                    for (int j = 0; j < len; j++) {
                        if (m[i][j] == 1) {
                            ans++;
                            clear(m, i, j);
                        }
                    }
                }
                return ans;
            }
    
            public void clear(int[][] m, int q, int p)
            {
                if (m[q][p] == 0) {
                    return;
                }
                m[q][p] = 0;
                for (int i = 0; i < len; i++) {
                    clear(m, i, p);
                }
                for (int i = 0; i < len; i++) {
                    clear(m, q, i);
                }
            }
        }

    2、广度优先搜索

    public class Location{
        int x;
        int y;
        Location(int m, int n){
            x = m;
            y = n;
        }
    }
    
    class Solution {
        int len;
        Queue<Location> queue = new LinkedList<Location>();
        public int findCircleNum(int[][] m) {
            len = m.length;
            int ans = 0;
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    if (m[i][j] != 0) {
                        ans++;
                        queue.add(new Location(i, j));
                        while (queue.size() != 0) {
                            Location l = queue.remove();
                            if (m[l.x][l.y] != 0) {
                                m[l.x][l.y] = 0;
                                for (int k = 0; k < len; k++) {
                                    queue.add(new Location(l.x, k));
                                    queue.add(new Location(k, l.y));
                                }
                            }
                        }
                    }
                }
            }
            return ans;
        }
    }

    3、并查集

        class Solution {
            int[] arr;
            int[] count;
            int allgroup = 0;
            public int findCircleNum(int[][] m) {
                int len = m.length;
                if (len == 0) {
                    return 0;
                }
                arr = new int[len];
                count = new int[len];
                allgroup = len;
                for (int i = 0; i < len; i++) {
                    arr[i] = i;
                    count[i] = 1;
                }
                for (int i = 0; i < len; i++) {
                    for (int j = 0; j < len; j++) {
                        if (m[i][j] == 0) {
                            continue;
                        }
                        int roota = find(i);
                        int rootb = find(j);
                        if (roota == rootb) {
                            continue;
                        }
                        allgroup--;
                        if (count[roota] > count[rootb]) {
                            arr[rootb] = roota;
                            count[roota] += count[rootb];
                        } else {
                            arr[roota] = rootb;
                            count[rootb] += count[roota];
                        }
                    }
                }
                return allgroup;
            }
            public int find(int a) {
                while (arr[a] != a) a = arr[a];
                return a;
            }
        }
  • 相关阅读:
    Log Explorer的使用
    Windows消息大全
    Devepress LayoutControl的使用
    IIS7虚拟目录出现HTTP错误500.19(由于权限不足而无法读取配置文件)
    检索参数信息并填充指定的 SqlCommand 对象的 Parameters 集合
    Failed to access IIS metabase.
    SQL Server FOR XML PATH 语句的应用
    C#调用API:mouse_event 模拟鼠标事件
    C#执行SQL脚本
    ADO.NET 中的表达式
  • 原文地址:https://www.cnblogs.com/tobemaster/p/12408552.html
Copyright © 2020-2023  润新知