bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 200. Number of Islands

200. Number of Islands

Leetcode Solutions

Approach #: Depth-First Search (DFS)

  1. Initialize a counter for the number of islands to 0.
  2. Iterate over each cell in the grid.
  3. If a cell's value is '1', increment the island counter and call DFS on that cell.
  4. In DFS, mark the current cell as '0' to indicate it has been visited, then recursively call DFS on all adjacent cells that are within bounds and have a value of '1'.
  5. Continue the process until all cells in the grid have been visited.
  6. Return the island counter as the result.
UML Thumbnail

Approach #: Breadth-First Search (BFS)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...