@alanwang If an island is others' neighbor, those islands are it's neighbors. So just caculates the top and left(or down and right) neighbors, and then sum plus 2. The same as @liupangzi 's solution.
@dcsds1 This issue was just fixed.
This is just non-recursive DFS. How is this a hash table solution.
No one has replied
It is indeed short but may not be very efficient:
if the island has a lot of inland cells, a lot of edges are counted twice.
The ideal case for your algorithm is a 1-cell island, in which no edges are doubly counted.
The worst case for your algorithm is a full-grid island, in which almost all edges are doubly counted.
P.S. That's my first solution as well, although not coded as neatly as how you brilliantly did.
Your runtime ?
It's runtime is 129ms could you suggest some ways to reduce it.
Disabled Categories are greyed out
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.