Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb9fd21

Browse files
committed
Max Area of Island
1 parent5c1dc3f commitfb9fd21

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎src/MaxAreaofIsland.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
publicclassMaxAreaofIsland {
3+
privateintre=0;
4+
publicintmaxAreaOfIsland(int[][]grid) {
5+
intn =grid.length;
6+
intm =grid[0].length;
7+
intmax =0;
8+
boolean[][]visited =newboolean[n][m];
9+
for(inti =0;i<n;i++){
10+
for(intj=0;j<m;j++){
11+
if(grid[i][j]==1 && !visited[i][j]){
12+
re=0;
13+
bfs(grid,i,j,visited);
14+
max=Math.max(max,re);
15+
}
16+
17+
}
18+
}
19+
returnmax;
20+
}
21+
22+
publicvoidbfs(int[][]grid,inti,intj,boolean[][]visited){
23+
if(i<0 ||i>=grid.length ||j<0 ||j >=grid[0].length)
24+
return;
25+
if(grid[i][j]==0 ||visited[i][j])return;
26+
27+
visited[i][j] =true;
28+
re++;
29+
30+
bfs(grid,i-1,j,visited);
31+
bfs(grid,i,j+1,visited);
32+
bfs(grid,i+1,j,visited );
33+
bfs(grid,i,j-1,visited);
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp