[力扣算法题]《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