[抄题]:

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);
}
}

733. Flood Fill 简单型染色问题的更多相关文章

  1. LN : leetcode 733 Flood Fill

    lc 733 Flood Fill 733 Flood Fill An image is represented by a 2-D array of integers, each integer re ...

  2. 【Leetcode_easy】733. Flood Fill

    problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...

  3. 【LeetCode】733. Flood Fill 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  4. [LeetCode&Python] Problem 733. Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  5. 733. Flood Fill

    class Solution { public: int szx,szy; vector<vector<int>> floodFill(vector<vector< ...

  6. Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)

    Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...

  7. 和同事谈谈Flood Fill 算法

    前言 今天忙完了公司的工作后,发现同事在做LeeCode的算法题,顿时来了兴趣,于是王子与同事一起探讨如何能做好算法题,今天在此文章中和大家分享一下. 什么是Flood Fill 算法 我们今天谈论的 ...

  8. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  9. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

随机推荐

  1. RedHat Server Enterprise 6安装G++

    RedHat 6默认是安装有GCC,而没有安装G++编译 要安装G++前最好先查看下GCC的版本号,通常GCC的版本和G++的版本是相同的,知道GCC的版本再去找G++的安装文件就容易些,版本号有在安 ...

  2. 真正明白c语言二级指针

    指针是C语言的灵魂,我想对于一级指针大家应该都很熟悉,也经常用到:比如说对于字符串的处理,函数参数的“值,结果传递”等,对于二级指针或者多级指针,我想理解起来也是比较容易的,比如二级指针就是指向指针的 ...

  3. bootstrap table 兼容ie8 -- refreshOptions

    今天项目使用 bootstrap table 在ie8下发现 方法 refreshOptions 报错. 经过调试监控发现错误如下: 153 行 代码 Object.getOwnPropertyNam ...

  4. AIX rcp跨主机远程

    rcp用途:在本地主机和远程主机之间或者两个远程主机之间传输文件.详细用法可man rcp查看. 现在要把主机10.200.5.200的/tmp/work.sh(属主为root用户)拷贝到远程主机18 ...

  5. Oracle实例 startup 报错:LRM-00109: could not open parameter file

    SQL> startup ORA-01078: failure in processing system parameters LRM-00109: could not open paramet ...

  6. java代码求输入数的平均值~~~~

    总结:1.谢谢程老师,一个很好的老师,人很普通,但是浑浊的世界里,那一份真实感动到底~~~~很感谢他 2.这里注意两个方面,也是我最大的弱点:循环和数组的length属性.前者运用不灵活,后者自己总是 ...

  7. juc线程池原理(六):jdk线程池中的设计模式

    一.jdk中默认线程池中的代理模式 单例类线程池只有一个线程,无边界队列,适合cpu密集的运算.jdk中创建线程池是通过Executors类中提供的静态的方法来创建的,其中的单例类线程池的方法如下: ...

  8. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  9. 微信小程序的基本认识

    小程序与公众号的区别 小程序,不支持关注,消息推送等营销手段. 小程序更倾向于产品,公众号更倾向于营销. 在系统权限方面,小程序能够获得更多. 小程序与APP的区别 小程序,面向微信用户.app面向所 ...

  10. python下载指定页面的所有图片

    实现步骤: 1.下载页面源码 2.对页面进行解析,获取页面中所有的图片路径 3.下载图片到指定路径 代码实例: # coding: utf-8 import urllib2 # 该模块用于打开页面地址 ...