|
| 1 | +<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> |
| 2 | + |
| 3 | +<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> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | + |
| 8 | +<pre> |
| 9 | +<strong>Input:</strong> grid = [ |
| 10 | + ["1","1","1","1","0"], |
| 11 | + ["1","1","0","1","0"], |
| 12 | + ["1","1","0","0","0"], |
| 13 | + ["0","0","0","0","0"] |
| 14 | +] |
| 15 | +<strong>Output:</strong> 1 |
| 16 | +</pre> |
| 17 | + |
| 18 | +<p><strong class="example">Example 2:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input:</strong> grid = [ |
| 22 | + ["1","1","0","0","0"], |
| 23 | + ["1","1","0","0","0"], |
| 24 | + ["0","0","1","0","0"], |
| 25 | + ["0","0","0","1","1"] |
| 26 | +] |
| 27 | +<strong>Output:</strong> 3 |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>m == grid.length</code></li> |
| 35 | + <li><code>n == grid[i].length</code></li> |
| 36 | + <li><code>1 <= m, n <= 300</code></li> |
| 37 | + <li><code>grid[i][j]</code> is <code>'0'</code> or <code>'1'</code>.</li> |
| 38 | +</ul> |
0 commit comments