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) 染别的点。不是空中楼阁
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 本题中,原色不想等 不是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 简单型染色问题的更多相关文章
- 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 ...
- 【Leetcode_easy】733. Flood Fill
problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...
- 【LeetCode】733. Flood Fill 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- [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 ...
- 733. Flood Fill
class Solution { public: int szx,szy; vector<vector<int>> floodFill(vector<vector< ...
- Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)
Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...
- 和同事谈谈Flood Fill 算法
前言 今天忙完了公司的工作后,发现同事在做LeeCode的算法题,顿时来了兴趣,于是王子与同事一起探讨如何能做好算法题,今天在此文章中和大家分享一下. 什么是Flood Fill 算法 我们今天谈论的 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
随机推荐
- easyui1.4 汉化出问题
easyui 1.4 的textbox 验证汉化不了,需要在easyui-lang-zh_CN.js 加入 if ($.fn.textbox){ $.fn.textbox.defaults.missi ...
- [转]Socket编程中,阻塞与非阻塞的区别
阻塞:一般的I/O操作可以在新建的流中运用.在服务器回应前它等待客户端发送一个空白的行.当会话结束时,服务器关闭流和客户端socket.如果在队列中没有请示将会出现什么情况呢?那个方法将会等待一个的到 ...
- ActionContext介绍(在Struts2中)
一种属性的有序序列,它们为驻留在环境内的对象定义环境.在对象的激活过程中创建上下文,对象被配置为要求某些自动服务,如同步.事务.实时激活.安全性等等.多个对象可以存留在一个上下文内.也有根据上下文理解 ...
- 关于matlab浮点转定点总结
1,算式长度不应该太长,否则在转换过程中提示位宽超过128位,(用的64位matlab),长算式改为短算式就可以了. 2,不要过于相信推荐字长,有些地方需要更高的精度,如果用推荐字长,可能结果误差较大 ...
- 对django整个网站搭建文件的总结
1.比方说现在是写一个使用HTTP 2.0的HTTP服务器,在不改变现有程序配置的情况下,需要重新编译HTTP服务器,比方说,用的是Nginx服务器,需要隐藏服务器版本,如果想要小绿锁 也就是网站安全 ...
- Dawn 阿里开源前端开发构建工具
Dawn 取「黎明.破晓」之意,原为「阿里云·业务运营团队」内部的前端构建和工程化工具,现已完全开源.它通过pipeline 和 middleware 将开发过程抽象为相对固定的阶段和有限的操作,简化 ...
- ASP.NET Cache缓存的使用
ASP.NET Cache是提升系统性能的重要方法,它使用了“最近使用”原则(a least-recently-used algorithm).在数据库访问中经常会用到Cache保存数据库数据. 1. ...
- java返回集合为null还是空集合
个人认为在自己写接口时,需要返回集合时返回一个空集合,比如mybatis查询如果返回一个集合,结果为空时也会返回一个空集合而不是null. 那么这样有什么好处呢?最大的好处就是调用方不用在判断是否为n ...
- UnQLite简介
UnQLite是,由Symisc Systems公司出品的一个嵌入式C语言软件库,它实现了一个自包含.无服务器.零配置.事务化的NoSQL数据库引擎.UnQLite是一个文档存储数据库,类似于Mong ...
- java代码getHostAddress .getHostName()的练习
总结:主机名,ip地址是可以实现的,关键是要掌握 package com.aa; import java.io.IOException; import java.net.*; public class ...