Package g0701_0800.s0733_flood_fill
Class Solution
java.lang.Object
g0701_0800.s0733_flood_fill.Solution
733 - Flood Fill.<p>Easy</p>
<p>An image is represented by an <code>m x n</code> integer grid <code>image</code> where <code>image[i][j]</code> represents the pixel value of the image.</p>
<p>You are also given three integers <code>sr</code>, <code>sc</code>, and <code>newColor</code>. You should perform a <strong>flood fill</strong> on the image starting from the pixel <code>image[sr][sc]</code>.</p>
<p>To perform a <strong>flood fill</strong> , consider the starting pixel, plus any pixels connected <strong>4-directionally</strong> to the starting pixel of the same color as the starting pixel, plus any pixels connected <strong>4-directionally</strong> to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with <code>newColor</code>.</p>
<p>Return <em>the modified image after performing the flood fill</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/01/flood1-grid.jpg" alt="" /></p>
<p><strong>Input:</strong> image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2</p>
<p><strong>Output:</strong> [[2,2,2],[2,2,0],[2,0,1]]</p>
<p><strong>Explanation:</strong> From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color. Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2</p>
<p><strong>Output:</strong> [[2,2,2],[2,2,2]]</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == image.length</code></li>
<li><code>n == image[i].length</code></li>
<li><code>1 <= m, n <= 50</code></li>
<li><code>0 <= image[i][j], newColor < 2<sup>16</sup></code></li>
<li><code>0 <= sr < m</code></li>
<li><code>0 <= sc < n</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
floodFill
public int[][] floodFill(int[][] image, int sr, int sc, int newColor)
-