Yu's Coding Garden : leetcode Questions: Number of Islands: "leetcode Questions: Number of Islands
Number of Islands
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
Analysis:
This is a typical recursive problem which can be solved by either Depth First Search(DFS) or Breath First Search (BFS).
Briefly speaking, DFS is to search every possible directions(solutions) whenever meet the new one, BFS is to search and store every possible directions(solutions) using a queue usually, then search from the head of the queue each time.
In this problem, possible directions are left, right, top, bottom, with constrains that the new location is '1' and has not been visited before. Therefore, we can define a bool matrix (of same size) to store the status of each element in the grid, set it to false when it is not available(been visited).
Details can be found in the code below, which is pretty straightforward."
'via Blog this'
Blog Archive
-
▼
2017
(115)
-
▼
January
(21)
- algorithm - Counting number of comparisons in Quic...
- 谷歌CEO皮查伊:语音搜索是最自然的交互 我们优势明显 - 未名空间(mitbbs.com)
- Count Inversions in an array | Set 1 (Using Merge ...
- Count Inversions in an array | Set 1 (Using Merge ...
- Algs4-1.4.18 Find local minimum in n x n matrix in...
- 理财不理财,差距究竟有多大? - 未名空间(mitbbs.com)
- Urban Dictionary: quote unquote
- Quick Sort
- What are the top 10 algorithms of the 20th century...
- Virgin Founder Reposts Fake Final Words of Steve J...
- How to install matlab on ubuntu
- rizar/attention-lvcsr: End-to-End Attention-Based ...
- How did Strassen come up with his famous Strassen ...
- Yu's Coding Garden : leetcode Questions: Number of...
- WA State Licensing (DOL) Official Site:Moving? Get...
- WA State Licensing (DOL) Official Site: Moving? Ge...
- Compliance and eDiscovery Solutions & Speech Recog...
- Zigin a better job
- 深度丨阿里云AI专家陈一宁:解密阿里云语音识别的计算方案、声学模型和产品落地
- Kaldi 入门详解 - 道老师机器学习专栏 - 博客频道 - CSDN.NET
- 怎样在北美成为中医师,针灸师?
-
▼
January
(21)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment