130. Surrounded Regions
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:
After running your function, the board should be:
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.
解题要点:
首先把边界的‘O'以及与其相邻的’O'全变为不有效字符(在此我用‘#’),来处理不被包围的’O'。然后把其他‘O'全部转换为’X'(被包围的‘O'),最后把‘#’改回‘O'。
Last updated
Was this helpful?