dfs学习之寻找朋友圈数

LeetCode题目 547. 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.

分析

这道题的意思是在一个n个人的集体中寻找出k个朋友圈。即通过深度搜索的方式通过一个人作为起点,然后一直深入地去寻找他们所有的朋友。一种做法是通过n*n矩阵来遍历,发现M[i][j]的值是1,则将该点作为起点,寻找i和j的所有朋友,然后找到之后把他们的记录都化成0。找完之后,在原来初始化的一个n*n矩阵返回该点之后的朋友关系数。其实就是起点记录他们之后找到了多少个朋友关系。在n*n次的遍历之后确保了所有的朋友记录M[i][j]都化成0,则结束遍历。结束了之后,自己原先初始化的矩阵中会记录着每次作为起点开始遍历的坐标,该点坐标不为零,其他的都是0。然后在这n*n矩阵中看有多少个不为零的起点,则有多少个朋友圈。这种做法思路比较简单,但是时间空间度都很大,时间复杂度为O(n*3),空间复杂度是O(n^2)。
然后参考了解开的都是套路 的思想,用一个1*n的数组记录访问记录。开始从0到n-1开始循环,发现该点没访问过就加入队列循环,发现他有其他的朋友并且该朋友没有被访问过就加进该队列,然后弹出队列的队头。直到队列为空,则得到一个朋友圈。这个方法的时间复杂度是O(n^2),空间复杂度是O(1),得到很大改善。

代码

第一种方法

class Solution {
public:
    int findFriends(vector<vector<int> >& M, int x, int y) {
        int nums = 1;
        M[x][y] = 0;
        int pos[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
        for (int i = 0; i < 4; i++) {
            int px = x + pos[i][0];
            int py = y + pos[i][1];
            if (px < 0 || px >= M.size()) continue;
            if (py < 0 || py >= M.size()) continue;
            if (M[px][py] == 1) nums += findFriends(M, x, y);
        }
        return nums;
    }


    int findCircleNum(vector<vector<int>>& M) {
        int _size = M.size();
        int nums = 0;
        nums = findFriends(M, nums);
        int** friends = new int*[_size];
        for (int i = 0; i < _size; i++) friends[i] = new int[_size];
        for (int i = 0; i < M.size(); i++) {
            for (int j = 0; j < M.size(); j++) {
                if (M[i][j] == 1)  friends[i][j] = findFriends(M, i, j);
            }
        }
        for (int i = 0; i < M.size(); i++) {
            for (int j = 0; j < M.size(); j++) {
                if (M[i][j] >= 1) nums++;
            }
        }
        return nums;
    }
};

第二种方法

using namespace std;
class Solution {
public:
    int findCircleNum(vector<vector<int>>& M) {
        int _size = M.size();
        int* visited = new int[_size];
        for (int i = 0; i < _size; i++) visited[i] = 0;
        queue<int> que;
        int nums = 0;
        for (int i = 0; i < _size; i++) {
            if (visited[i] == 0) {
                que.push(i);
                visited[i] = 1;
                nums++;
                while (!que.empty()) {
                    int pos = que.front();
                    que.pop();
                    for (int j = 0; j < _size; j++) {
                        if (visited[j] == 0 && M[pos][j] == 1) {
                            que.push(j);
                            visited[j] = 1;
                        }
                    }
                }
            }
        }
        delete []visited;
        return nums;
    }
};

代码地址:
https://leetcode.com/problems/friend-circles/description/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值