
| # | Title | Difficulty | Topic |
|---|---|---|---|
| 130 | Surrounded Regions | Medium | Depth-first Search; Breadth-first Search; Union Find |
Description
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
Example:
1 |
X X X X |
After running your function, the board should be:
1 |
X X X X |
Explanation:
Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
两次替换
先利用DFS将边缘的O替换为W。然后,再将内部的O替换为X,再将边缘的W替换为O。
1 |
class { |




近期评论