Whenever possible, steal code.
Keep it simple, stupid.
– Programming Pearls, Jon Bentley.
Problem
Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island 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.
Java
BFS
1
1 | import java.util.LinkedList; |
Submission Detail
- 47 / 47 test cases passed.
- Runtime: 10 ms, faster than 5.88% of Java online submissions for Number of Islands.
- Memory Usage: 41.6 MB, less than 55.82% of Java online submissions for Number of Islands.
2
1 | class Solution { |
Submission Detail
- 47 / 47 test cases passed.
- Runtime: 5 ms, faster than 15.24% of Java online submissions for Number of Islands.
- Memory Usage: 42.3 MB, less than 17.21% of Java online submissions for Number of Islands.
union–find
1 | class Solution { |
Submission Detail
- 47 / 47 test cases passed.
- Runtime: 28 ms, faster than 5.05% of Java online submissions for Number of Islands.
- Memory Usage: 45.3 MB, less than 5.12% of Java online submissions for Number of Islands.