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

Commit fb9fd21

Browse files
Max Area of Island
1 parent 5c1dc3f commit fb9fd21

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+
public class MaxAreaofIsland {
3+
private int re=0;
4+
public int maxAreaOfIsland(int[][] grid) {
5+
int n = grid.length;
6+
int m = grid[0].length;
7+
int max =0;
8+
boolean[][] visited = new boolean[n][m];
9+
for(int i = 0; i< n; i++){
10+
for(int j=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+
return max;
20+
}
21+
22+
public void bfs(int[][] grid, int i, int j, 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)

AltStyle によって変換されたページ (->オリジナル) /