Coding Round 2
Mid
programming
LeetCode #200 - Number of Islands
Background: Given a 2D grid 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 surrounded by water.
Problem Statement: Implement a function
Example 1:
Input: grid = [ ['1','1','1','1','0'], ['1','1','0','1','0'], ['1','1','0','0','0'], ['0','0','0','0','0'] ]
Output: 1
Example 2:
Input: grid = [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ]
Output: 3
Constraints:
Problem Statement: Implement a function
int numIslands(char[][] grid) that returns the number of islands in the given grid. Example 1:
Input: grid = [ ['1','1','1','1','0'], ['1','1','0','1','0'], ['1','1','0','0','0'], ['0','0','0','0','0'] ]
Output: 1
Example 2:
Input: grid = [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ]
Output: 3
Constraints:
m == grid.length, n == grid[i].length, 1 <= m, n <= 300.
Suggested Answer