-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1020.txt
More file actions
37 lines (34 loc) · 777 Bytes
/
1020.txt
File metadata and controls
37 lines (34 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
思路:
岛屿变种题
代码:
class Solution {
int cnt=0;
boolean check=false;
public int numEnclaves(int[][] A) {
int res=0;
for(int i=0;i<A.length;i++){
for(int j=0;j<A[0].length;j++){
if(A[i][j]==1){
cnt=0;
check=false;
dfs(A,i,j);
if(!check)res+=cnt;
}
}
}
return res;
}
public void dfs(int A[][],int i,int j){
if(i<0||j<0||i>=A.length||j>=A[0].length){
check=true;
return;
}
if(A[i][j]==0)return;
A[i][j]=0;
cnt++;
dfs(A,i+1,j);
dfs(A,i-1,j);
dfs(A,i,j+1);
dfs(A,i,j-1);
}
}