547. 省份的数量

547. 省份数量

难度:中等

n 个城市,其中一些彼此相连,另一些没有相连。如果城市 a 与城市 b 直接相连,且城市 b 与城市 c 直接相连,那么城市 a 与城市 c 间接相连。

省份 是一组直接或间接相连的城市,组内不含其他没有相连的城市。

给你一个 n x n 的矩阵 isConnected ,其中 isConnected[i][j] = 1 表示第 i个城市和第 j 个城市直接相连,而 isConnected[i][j] = 0 表示二者不直接相连。

返回矩阵中 省份 的数量。

示例 1:

img

输入:isConnected = [[1,1,0],[1,1,0],[0,0,1]]
输出:2

示例 2:

img

输入:isConnected = [[1,0,0],[0,1,0],[0,0,1]]
输出:3

提示:

  • 1 <= n <= 200
  • n == isConnected.length
  • n == isConnected[i].length
  • isConnected[i][j]10
  • isConnected[i][i] == 1
  • isConnected[i][j] == isConnected[j][i]

解答:

class UnionFind{
    int[] parent;
    
    public UnionFind(int n){
        parent = new int[n];
        for(int i = 0; i < n; i++){
            parent[i] = i;
        }
    }
    
    public int find(int index){
        if(parent[index] != index){
            parent[index] = find(parent[index]);
        }
        return parent[index];
    }
    
    public void union(int i, int j){
        parent[find(i)] = find(j);
    }
    
    public int getCount(){
        int count = 0;
        for(int i = 0; i < parent.length; i++){
            if(parent[i] == i){
                count++;
            }
        }
        return count;
    }
}

class Solution {
    //并查集
    //时间复杂度O(N^2logN),空间复杂度O(N)    
    public int findCircleNum(int[][] isConnected) {
        int n = isConnected.length;
        UnionFind uf = new UnionFind(n);
        for(int i = 0; i < n; i++){
            for(int j = i + 1; j < n; j++){
                if(isConnected[i][j] == 1){
                    uf.union(i, j);
                }
            }
        }
        return uf.getCount();
    }
}

class Solution {
    //DFS
    //时间复杂度O(n^2),空间复杂度O(n)
    public int findCircleNum(int[][] isConnected) {
        int n = isConnected.length;
        boolean[] visited = new boolean[n];
        int count = 0;
        for(int i = 0; i < n; i++){
            if(!visited[i]){
                dfs(isConnected, visited, n, i);
                count++;
            }
        }
        return count;
    }
    
    public void dfs(int[][] isConnected, boolean[] visited, int n, int i){
        for(int j = 0; j < n; j++){
            if(isConnected[i][j] == 1 && !visited[j]){
                visited[j] = true;
                dfs(isConnected, visited, n, j);
            }
        }
    }
}

class Solution {
    //BFS
    //时间复杂度O(n^2),空间复杂度O(n)
    public int findCircleNum(int[][] isConnected) {
        int n = isConnected.length;
        boolean[] visited = new boolean[n];
        int count = 0;
        Queue<Integer> queue = new LinkedList<>();
        for(int i = 0; i < n; i++){
            if(!visited[i]){
                count++;
                queue.offer(i);
                bfs(queue, isConnected, visited, n);
            }
        }
        return count;
    }
    
    public void bfs(Queue<Integer> queue, int[][] isConnected, boolean[] visited, int n){
        while(!queue.isEmpty()){
            int i = queue.poll();
            visited[i] = true;
            for(int j = 0; j < n; j++){
                if(isConnected[i][j] == 1 && !visited[j]){
                    queue.offer(j);
                }
            }
        }
    }
}

参考自:

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/number-of-provinces/solution/sheng-fen-shu-liang-by-leetcode-solution-eyk0/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(LeetCode,leetcode,算法)