Class Solution

java.lang.Object
g0101_0200.s0130_surrounded_regions.Solution

public class Solution extends Object
130 - Surrounded Regions.<p>Medium</p> <p>Given an <code>m x n</code> matrix <code>board</code> containing <code>'X'</code> and <code>'O'</code>, <em>capture all regions that are 4-directionally surrounded by</em> <code>'X'</code>.</p> <p>A region is <strong>captured</strong> by flipping all <code>'O'</code>s into <code>'X'</code>s in that surrounded region.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg" alt="" /></p> <p><strong>Input:</strong> board = [[&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;],[&ldquo;X&rdquo;,&ldquo;O&rdquo;,&ldquo;O&rdquo;,&ldquo;X&rdquo;],[&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;O&rdquo;,&ldquo;X&rdquo;],[&ldquo;X&rdquo;,&ldquo;O&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;]]</p> <p><strong>Output:</strong> [[&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;],[&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;],[&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;],[&ldquo;X&rdquo;,&ldquo;O&rdquo;,&ldquo;X&rdquo;,&ldquo;X&rdquo;]]</p> <p><strong>Explanation:</strong> Surrounded regions should not be on the border, which means that any &lsquo;O&rsquo; on the border of the board are not flipped to &lsquo;X&rsquo;. Any &lsquo;O&rsquo; that is not on the border and it is not connected to an &lsquo;O&rsquo; on the border will be flipped to &lsquo;X&rsquo;. Two cells are connected if they are adjacent cells connected horizontally or vertically.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> board = [[&ldquo;X&rdquo;]]</p> <p><strong>Output:</strong> <a href="&quot;X&quot;">&quot;X&quot;</a></p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == board.length</code></li> <li><code>n == board[i].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>board[i][j]</code> is <code>'X'</code> or <code>'O'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • solve

      public void solve(char[][] board)