zoukankan      html  css  js  c++  java
  • 733. Flood Fill 简单型染色问题

    [抄题]:

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

    Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.

    To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.

    At the end, return the modified image.

    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.
    

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    忘了dfs的模板格式了,以为退出条件要分开写。但其实退出条件是提前写到一起的。

    [一句话思路]:

    先染头一个点,形成基础,再四周扩展(还是0 to n-1) 染别的点。不是空中楼阁

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 本题中,原色不想等 不是color的点不能染 。退出条件中,范围在前,特殊情况在后。

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    退出条件中,范围在前,特殊情况在后。

    [复杂度]:Time complexity: O() Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    退出条件 范围在前

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
            //cc
            if (image[sr][sc] == newColor) return image;
            
            //dfs
            dfs(image, sr, sc, image[sr][sc], newColor);
            
            //return
            return image;
        }
        
        public void dfs(int[][] image, int sr, int sc, int color, int newColor) {
            //exit;
            if (sr < 0 || sr >= image.length || sc < 0 || sc>= image[0].length || color != image[sr][sc]) return ;
            
            //first color
            image[sr][sc] = newColor;
            
            //expand
            dfs(image, sr + 1, sc, color, newColor);
            dfs(image, sr - 1, sc, color, newColor);
            dfs(image, sr, sc + 1, color, newColor);
            dfs(image, sr, sc - 1, color, newColor);
        }
    }
    View Code
  • 相关阅读:
    让windows10的右键菜单既显示传统cmd又显示powershell
    配置php环境的一个nginx.conf
    windows中启动和终止nginx的两个批处理
    WxWidgets笔记
    安装archlinux的linux命令记录
    window中的attrib命令
    docker中i的作用
    airflow中的两个参数
    sqlite数据库中为字段设置默认值为当前时间
    记oracle使用expdp将数据导出到asm报错
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8971361.html
Copyright © 2011-2022 走看看