Description
Difficulty: Medium
You’re given a two-dimensional array (a matrix) of potentially unequal height and width containing only 0s and 1s. Each 0 represents land and each 1 represents part of a river. A river consists of any number of 1s that are either horizontally or vertically adjacent (but not diagonally adjacent). The number of adjacent 1s forming a river determine its size.
Note that a river can twist. In other words, it doesn’t have to be a straight vertical line or a straight horizontal line; it can be L-shaped, for example.
Write a function that returns an array of the sizes of all rivers represented in the input matrix. The sizes don’t need to be in any particular order.
Example:
1
2
3
4
5
6
7
8 matrix: [
[1,0,0,1,0],
[1,0,1,0,0],
[0,0,1,0,1],
[1,0,1,0,1],
[1,0,1,1,0],
]
output = [1,2,2,2,5]
Solution - 1:DFS
很典型的DFS或者BFS的题目。
1 | import java.util.*; |
Summary
- DFS用过好几次,但是每道题的递归和return还是有所区别。
总之希望自己能坚持下去,每周记录分享几道有趣的题和解法。也欢迎大家留言讨论补充(●’◡’●)