力扣算法之《547. 省份数量》

[力扣算法题]《547. 省份数量》

class Solution {
    public static int findCircleNum(int[][] isConnected) {
        if(isConnected == null)return 0;
        int ans = 0;
        int len = isConnected.length;
        boolean []visited = new boolean[len];
        // boolean []visited = new boolean[len](); // error 默认为false
        for(int i = 0; i < len; ++i){
            if(!visited[i]){
                dfs(isConnected, visited, len, i);
                ans++;
            }
        }
        
        return ans; // error 4: no change the real return value;
    }
    
    public static void dfs(int [][]isConnected, boolean []visited, int len, int i){
        // if(visited[i] == true)return; // error 3:not need
        for(int j = 0; j < len; ++j){
            if(isConnected[i][j] == 1 && !visited[j]){
            // if(isConnected[i][j] == 1){ // error 1: the lack of the consideration full critical condition 
                // isConnected[i][j] = 0; // // error 3:not need
                
                visited[i] = true;
                dfs(isConnected, visited, len, j);
                
                // dfs(isConnected, visited, len, j);// error 2:lack of the recursion
            }
        }
        // return 1; // error 3:not need
    }
    


    
    
//     // the first type following the official website
//     public static void dfs(int [][]isConnected, boolean []visited, int len, int i){
//         // for(int j = 0, j < len, ++j){
//         for(int j = 0; j < len; ++j){
//             if(isConnected[i][j] == 1 && !visited[j]){
//             // if(isConnected[i][j] == 1 && !visited[j])
//             // if(isConnected[i][j] == 1){
//                 visited[j] = true;
//                 dfs(isConnected, visited, len,  j);
//             }
//         }
//     }
    
//     public static int findCircleNum(int[][] isConnected) {
//         int len = isConnected.length;
//         // boolean []visited = new boolean[];
//         // boolean []visited = false;
//         boolean []visited = new boolean[len];
//         int ans = 0;
//         for(int i = 0; i < len; ++i){
//             if(!visited[i]){
//                 dfs(isConnected, visited, len, i);
//                 ans++;
//                 // ans++;
//                 // dfs(isConnected, visited, len, i);
//             }
//         }
//         return ans;
        
        
//         // error
//         // if(isConnected == null || isConnected.length == 0)return 0;
//         // int rl = isConnected.length;
//         // int cl = isConnected[0].length;
//         // int ans = 0;
//         // for(int i = 0; i < rl; ++i){
//         //     for(int j = 0; j < cl && j != i; ++j){
//         //     if(isConnected[i][j] == 1)
//         //         isConnected[j][i] = 0;
//         //     }
//         // }
//         // for(int i = 0; i < rl; ++i){
//         //     for(int j = 0; j < cl && j != i; ++j){
//         //         // if(isConnected[r][c] == 1){
//         //         if(isConnected[i][j] == 0){
//         //             ans++;
//         //             dfs(isConnected, i, j);
//         //         }
//         //     }
//         // }
//         // // error: none return
//         // return ans;
//     }
//     // the first type following the official website    
        
    
    // public static void dfs(int [][] isConnected, int r, int c){
    //     // 深度优先遍历
    //     int rl = isConnected.length;
    //     int cl = isConnected[0].length;
    //     if(r < 0 || r >= rl || c < 0 || c >= cl || isConnected[r][c] == 1)return;
    //     isConnected[r][c] = 1;
    //     // isConnected[c][r] = 0;
    //     dfs(isConnected, r - 1, c);
    //     dfs(isConnected, r + 1, c);
    //     dfs(isConnected, r, c - 1);
    //     dfs(isConnected, r, c + 1);
    // }
    // public static void wfs(int [][] isConnected, int r, int c){
    //     // 广度优先遍历
    //     int rl = isConnected.length;
    //     int cl = isConnected[0].length;
    //     if(r < 0 || r >= rl || c < 0 || c >= cl || isConnected[r][c] == 0)return;
    //     isConnected[r][c] = 0;
    //     isConnected[c][r] = 0;
    //     wfs(isConnected, r, c - 1);
    //     wfs(isConnected, r, c + 1);
    //     wfs(isConnected, r - 1, c);
    //     wfs(isConnected, r + 1, c);
    // }
}

在这里插入图片描述

这个题的第二次代码复现真的难!! 😦

the 3rd try

    // the third try
    public static int findCircleNum(int[][] isConnected) {
        if(isConnected.length == 0 || isConnected == null)return 0;
        int ans = 0;
        int len = isConnected.length;
        boolean [] visited = new boolean[len];
        for(int i = 0; i < len; ++i){
            if(!visited[i]){ 
            // if(!visited(i)){  // error 3
                dfs(isConnected, len, visited, i);
                ans++;
            }
        }
        return ans;
    }
    public static void dfs(int [][]isConnected, int len, boolean[] visited, int i){
        // for(int j = 0; j < len, ++j){  // error 1
        for(int j = 0; j < len; ++j){
            if(isConnected[i][j] == 1 && !visited[j]){
                // visited[j] = 0; // error 2
                visited[j] = true;
                dfs(isConnected, len, visited, j);
            }
        }
    }
    
    // the third try
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值