2017年5月5日 星期五

[LeetCode] 200. Number of Islands

轉自LeetCode

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.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
<Solution>
典型的 DFS 題目,想法如下
  • 找到入口點後,把所有連在一起的 '1' 都找出來,並換成其他字元
  • 每找到一次入口點,答案就加 1
code 如下

C++

Java

kotlin

沒有留言:

張貼留言