2022-03-27 20:56:26 +08:00
< p > Given an < code > m x n< / code > 2D binary grid < code > grid< / code > which represents a map of < code > ' 1' < / code > s (land) and < code > ' 0' < / code > s (water), return < em > the number of islands< / em > .< / p >
< p > An < strong > island< / strong > 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.< / p >
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
< strong > Input:< / strong > grid = [
[" 1" ," 1" ," 1" ," 1" ," 0" ],
[" 1" ," 1" ," 0" ," 1" ," 0" ],
[" 1" ," 1" ," 0" ," 0" ," 0" ],
[" 0" ," 0" ," 0" ," 0" ," 0" ]
]
< strong > Output:< / strong > 1
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
< strong > Input:< / strong > grid = [
[" 1" ," 1" ," 0" ," 0" ," 0" ],
[" 1" ," 1" ," 0" ," 0" ," 0" ],
[" 0" ," 0" ," 1" ," 0" ," 0" ],
[" 0" ," 0" ," 0" ," 1" ," 1" ]
]
< strong > Output:< / strong > 3
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > m == grid.length< / code > < / li >
< li > < code > n == grid[i].length< / code > < / li >
< li > < code > 1 < = m, n < = 300< / code > < / li >
< li > < code > grid[i][j]< / code > is < code > ' 0' < / code > or < code > ' 1' < / code > .< / li >
< / ul >