Practice/Meta/Leetcode 200. Number of Islands
CodingMust
You are given a 2D grid representing a map where each cell contains either "1" (land) or "0" (water). An island is defined as a group of adjacent land cells connected horizontally or vertically (not diagonally). The grid is completely surrounded by water on all edges.
Your task is to count the total number of distinct islands in the grid.
1 <= grid.length <= 300 (number of rows)1 <= grid[0].length <= 300 (number of columns)grid[i][j] is either "1" or "0"Example 1:
Input: grid = [ ["1","1","0","0"], ["1","1","0","0"], ["0","0","1","0"], ["0","0","0","1"] ] Output: 3 Explanation: The grid contains three separate islands. The first island is the 2×2 block of land in the top-left corner. The second is a single cell at row 2, column 2. The third is a single cell at row 3, column 3.
Example 2:
Input: grid = [ ["1","0","1","0","1"], ["0","0","0","0","0"], ["1","0","1","0","1"] ] Output: 6 Explanation: Six isolated single-cell islands exist, arranged in a pattern with water separating each one.
Example 3:
Input: grid = [ ["1","1","1"], ["0","1","0"], ["1","1","1"] ] Output: 1 Explanation: All land cells are connected through the center cell, forming one large cross-shaped island.