#leetcode#547. Friend Circles

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ChiBaoNeLiuLiuNi/article/details/73234985

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 directfriends 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:

  1. N is in range [1,200].
  2. M[i][i] = 1 for all students.

  1. If M[i][j] = 1, then M[j][i] = 1.
=======================================================================

Two Sigma  的题,考察 Union Find, 和Number of islands ii 基本一样

由于对称性,只遍历右上角的点就够了,另外对角线上的点也可略过,直接把n加到结果里面,然后没发现需要union则 result--

union method犯了错,应该把两个点的root连接起来
public class Solution {
    public int findCircleNum(int[][] M) {
        int res = 0;
        if(M == null || M.length == 0 || M[0].length == 0)
            return res;
        int n = M.length;
        int[] roots = new int[n];
        Arrays.fill(roots, -1);  //here we init the roots array to current index itself, because M[i][i] = 1 for all students.
        
        res = n;
        for(int i = 0; i < n; i++){
            for(int j = i + 1; j < n; j++){//only top right half is enough to include all the cycles
                if(M[i][j] != 1)
                    continue;
                int rootI = findRoot(roots, i);
                int rootJ = findRoot(roots, j);
                if(rootI != rootJ){
                    // union(roots, i, j);
                    roots[rootJ] = rootI;  //直接在这里做union的操作了,否则union method中还要重复计算rootI,rootJ
                    res--;
                }
                
            }
        }
        
        return res;
    }
    
    private void union(int[] roots, int i, int j){//keep in mind that union is to union the roots of both position
        // roots[j] = roots[i];   ERROR in union method!!!!!!!!!!!!!
        // roots[j] = i;
        int rootJ = findRoot(roots, j);
        int rootI = findRoot(roots, i);
        roots[rootJ] = rootI;
    }
    
    private int findRoot(int[] roots, int i){
        if(roots[i] == -1)
            return i;
        
        int root = findRoot(roots, roots[i]);
        roots[i] = root;
        return root;
    }
}

猜你喜欢

转载自blog.csdn.net/ChiBaoNeLiuLiuNi/article/details/73234985