朋友圈的数量

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.
public int findCircleNum(int[][] M){
        int circleNum = 0;
        boolean[] hasVisited = new boolean[M.length];
        for(int i=0;i<M.length;i++){
            if(!hasVisited[i]){
                dfs(M, i, hasVisited);
                circleNum++;
            }
        }
        return circleNum;
    }
    
    private void dfs(int[][] M,int i,boolean[] hasVisited){
        hasVisited[i] = true;
        for(int k=0;k<M.length;k++){
            if(M[i][k] == 1 && !hasVisited[k]){
                dfs(M, k, hasVisited);
            }
        }
    }
原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/13446265.html