200. Number of Islands java solutions

Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110110101100000000

Answer: 1

Example 2:

11000110000010000011

Answer: 3

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

 1 public class Solution {
 2     public int numIslands(char[][] grid) {
 3         if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;
 4         int m = grid.length, n = grid[0].length;
 5         boolean[][] visit = new boolean[m][n];
 6         int ans = 0;
 7         for(int i = 0; i < m;i++){
 8             for(int j = 0; j < n; j++){
 9                 if(visit[i][j] != true && grid[i][j] == ‘1‘){
10                     ans++;
11                     BFS(visit,grid,i,j);
12                 }
13             }
14         }
15         return ans;
16     }
17
18     public void BFS(boolean[][] visit, char[][] grid, int row, int col){
19         if(row >=0 && row < visit.length && col >=0 && col < visit[0].length
20             && visit[row][col] != true && grid[row][col] == ‘1‘){
21             visit[row][col] = true;
22             BFS(visit,grid,row-1,col);//上下左右
23             BFS(visit,grid,row+1,col);
24             BFS(visit,grid,row,col-1);
25             BFS(visit,grid,row,col+1);
26         }
27     }
28 }
时间: 2024-08-26 02:52:07

200. Number of Islands java solutions的相关文章

【LeetCode】200. Number of Islands (2 solutions)

Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are

[LeetCode] 200. Number of Islands 岛屿的数量

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

200. Number of Islands

https://leetcode.com/problems/number-of-islands/#/description Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Yo

Leetcode 200. Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[LeetCode] 200. Number of Islands 解题思路

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[leedcode 200] Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

&lt;LeetCode OJ&gt; 200. Number of Islands

Total Accepted: 48411 Total Submissions: 171609 Difficulty: Medium Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or verticall

leetcode 200. Number of Islands(DFS)

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[email&#160;protected] [200] Number of Islands

https://leetcode.com/problems/number-of-islands/ Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume