Number of Islands Medium / #200 / grid
You get a map of land and water cells. Land cells that touch by a side form one island; touching by a corner does not count. Count the islands. Our map has 4 rows: # . # ., # . # ., # # # ., . . . # (# is land) — the answer is 2: the first seven land cells make one U-shaped island, and the cell in the bottom right corner is alone.
input grid: an m × n map, each cell '1' for land or '0' for water. returns The number of islands: groups of land cells joined up, down, left or right, not diagonally. Beyond the edges of the map is water. constraints m = grid.length, n = grid[i].length, 1 ≤ m, n ≤ 25 grid[i][j] is '0' or '1' our 16 tests keep to them examples Open a test in the tests panel: it shows the input and the expected answer. walkthrough solve timer --:-- off
switch language → JavaScript TypeScript Python Go Java submit only: no test in the browserC++ submit only: no test in the browserother /**
* @param {character[][]} grid
* @return {number}
*/
function numIslands(grid) {
}
test submit
16 tests · limit 3 s
01 — grid = [["1", "1", "0", "0", "1"], ["1", "0", "0", "1", "1"], ["0", "0", "1", "0", "0"]] copy 02 — grid = [["0", "0"], ["0", "0"]] copy 03 — grid = [["1"]] copy 04 — grid = [["1", "0", "1"], ["0", "1", "0"], ["1", "0", "1"]] copy 05 — grid = [["1", "0", "1", "1", "0", "1"]] copy 06 — grid = [["1"], ["1"], ["0"], ["1"]] copy 07 — grid = [["1", "1", "1"], ["1", "0", "1"], ["1", "1", "1"]] copy 08 — grid = [["1", "1", "1", "1", "1"], ["1", "0", "0", "0", "1"], ["1", "0", "1", "0", "1"], ["1", "0", "0", "0", "1"], ["1", "1", "1", "1", "1"]] copy 09 — grid = [["1", "0", "1", "0", "0", "1", "0", "1", "0", "1", "0", "0"], ["0", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0"], ["0", "1", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0"], ["0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "1"], ["1", "0", "0", "1", "0", "0", "0", "0", "1", "0", "1", "1"], ["0", "1", "0", "0", "0", "0", "1", "1", "0", "1", "0", "1"], ["0", "0", "1", "0", "0", "0", "1", "0", "1", "0", "0", "0"], ["1", "1", "1", "0", "1", "1", "1", "1", "1", "0", "1", "1"], ["0", "0", "1", "0", "0", "1", "0", "0", "1", "0", "0", "0"], ["1", "0", "0", "0", "0", "0", "1", "1", "1", "0", "0", "1"]] copy 10 — grid = [["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "0", … 25 strings], ["0", "0", "0", "0", "0", "0", "1", "0", "0", "1", "0", "1", … 25 strings], ["1", "0", "0", "1", "0", "1", "1", "0", "0", "0", "1", "0", … 25 strings], ["0", "1", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", … 25 strings], ["0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "1", … 25 strings], ["0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["0", "0", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["1", "0", "0", "1", "0", "0", "1", "0", "0", "1", "0", "0", … 25 strings], ["0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], … 25 arrays] copy 11 — grid = [["0", "1", "0", "0", "0", "1", "1", "1", "1", "1", "0", "0", … 25 strings], ["1", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "0", "0", "1", "1", "1", "1", "0", … 25 strings], ["1", "0", "1", "1", "0", "1", "0", "0", "0", "1", "1", "0", … 25 strings], ["0", "1", "1", "1", "1", "0", "1", "1", "1", "0", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "0", "1", "1", "0", "1", "1", "1", … 25 strings], ["1", "1", "0", "1", "0", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "0", "1", "1", "0", "0", "1", … 25 strings], ["0", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", … 25 strings], ["0", "0", "1", "0", "1", "0", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "0", "0", "1", "1", "1", "1", "0", "0", "1", "0", "1", … 25 strings], ["1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "0", "0", … 25 strings], … 25 arrays] copy 12 — grid = [["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], … 25 arrays] copy 13 — grid = [["1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", … 25 strings], ["0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", … 25 strings], ["1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", … 25 strings], ["0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", … 25 strings], ["1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", … 25 strings], ["0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", … 25 strings], ["1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", … 25 strings], ["0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", … 25 strings], ["1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", … 25 strings], ["0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", … 25 strings], ["1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", … 25 strings], ["0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", … 25 strings], … 25 arrays] copy 14 — grid = [["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 25 strings], ["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", … 25 strings], … 25 arrays] copy 15 — grid = [["1", "1", "1", "0", "1", "0", "0", "0", "1", "0", "1", "0", … 25 strings]] copy 16 — grid = [["0"], ["1"], ["0"], ["1"], ["0"], ["1"], ["0"], ["0"], ["1"], ["0"], ["1"], ["1"], … 25 arrays] copy
Stuck? See the solution run step by step on our example, then why it works and its complexity.
Watch the walkthrough → The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.