首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LeetCode: FloodFill C#

LeetCode: FloodFill C#
EN

Code Review用户
提问于 2020-04-10 13:22:01
回答 1查看 173关注 0票数 1

https://leetcode.com/problems/flood-fill/

图像由二维整数数组表示,每个整数表示图像的像素值(从0到65535)。给定表示洪水填充的起始像素(行和列)的坐标(sr,sc)和像素值newColor,“洪水填充”图像。要执行“洪流填充”,请考虑起始像素,再加上与起始像素相同颜色的起始像素4方向连接的任何像素,再加上与这些像素定向连接的任何像素(也与起始像素的颜色相同),等等。用newColor替换上述所有像素的颜色。最后,返回修改后的图像。

代码语言:javascript
复制
Example 1:
Input: 
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: 
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected 
by a path of the same color as the starting pixel 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.
Note:

The length of image and image[0] will be in the range [1, 50].
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

请检查性能和编码风格。

代码语言:javascript
复制
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions
{
    /// <summary>
    /// https://leetcode.com/problems/flood-fill/
    /// </summary>
    [TestClass]
    public class FloodFillTest
    {
        [TestMethod]
        public void ExampleTest()
        {
            int[][] image = { new[] { 1, 1, 1 }, new[] { 1, 1, 0 }, new[] { 1, 0, 1 } };
            int sr = 1;
            int sc = 1;
            int newColor = 2;
            int[][] expected = { new[] { 2, 2, 2 }, new[] { 2, 2, 0 }, new[] { 2, 0, 1 } };
            FloodFillDFS dfs = new FloodFillDFS();
            dfs.FloodFill(image, sr, sc, newColor);
            for (int i = 0; i < 3; i++)
            {
                CollectionAssert.AreEqual(expected[i],image[i] );
            }

        }
    }

    public class FloodFillDFS
    {
        public int[][] FloodFill(int[][] image, int sr, int sc, int newColor)
        {
            int oldColor = image[sr][sc];
            DFS(image, sr, sc, newColor, oldColor);
            return image;
        }

        //make sure to check first the corner cases
        private void DFS(int[][] image, int sr, int sc, int newColor, int oldColor)
        {
            if (sr < 0 || sc < 0 || sr >= image.Length || sc >= image[0].Length || image[sr][sc] == newColor || image[sr][sc] != oldColor)
            {
                return;
            }

            image[sr][sc] = newColor;
            DFS(image, sr - 1, sc, newColor, oldColor);
            DFS(image, sr + 1, sc, newColor, oldColor);
            DFS(image, sr, sc - 1, newColor, oldColor);
            DFS(image, sr, sc + 1, newColor, oldColor);
        }
    }
}
EN

回答 1

Code Review用户

回答已采纳

发布于 2020-04-10 18:01:44

我会将if除以3个变量,并给每个变量取一个名称: out映像、isVisited和isOriginalColor或类似的名称。

我不知道sr是什么意思,我会用行和行。

FloodFill返回图像,但是dfs改变了原始图像,它会混淆类的用户。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/240274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档