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.
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if (image[sr][sc] == newColor) return image;
fill(image, sr, sc, image[sr][sc], newColor);
return image;
} private void fill(int[][] image, int sr, int sc, int color, int newColor) {
if (sr < || sr >= image.length || sc < || sc >= image[].length || image[sr][sc] != color) return;
image[sr][sc] = newColor;
fill(image, sr + , sc, color, newColor);
fill(image, sr - , sc, color, newColor);
fill(image, sr, sc + , color, newColor);
fill(image, sr, sc - , color, newColor);
}
}
Flood Fill的更多相关文章
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- [Swift]LeetCode733. 图像渲染 | Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- LeetCode刷题 Flood Fill 洪水填充问题
An image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...
- [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 ...
- LeetCode - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- CF 1114 D. Flood Fill
D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...
- Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】
任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory ...
- 733. Flood Fill 简单型染色问题
[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value ...
随机推荐
- python创建文件夹方法
def mkdir(path): # 引入模块 import os # 去除首位空格 path = path.strip() # 去除尾部 \ 符号 path = path.rstrip(" ...
- vue整合adminLTE
前端框架AdminLTE 中文教程 如何用vue整合adminlte模板 1.adminlte 下载地址 : https://github.com/almasaeed2010/AdminLTE/rel ...
- 使用matplotlib绘制常用图表(2)-常用图标设置
一.使用subplots绘制子图 import numpy as np from matplotlib import pyplot as plt %matplotlib inline x = np.a ...
- 关于vue.js element ui 表单验证 this.$refs[formName].validate()的问题
方法使用前需了解: 来自”和“小编的小提示: 首先打印一下this.$refs[formName],检查是否拿到了正确的需要验证的form. 其次在拿到了正确的form后,检查该form上添加 ...
- 8.4 JavaScript(一)
参考链接:http://how2j.cn/k/javascript/javascript-javascript-tutorial/519.html 一.JavaScript是什么 JavaScript ...
- pl/sql 过程 函数(写一个过程,输入部门编号,在控制台打印这个部门的名称,总人数,平均工资(基本工资+奖金))
1.编写过程,输入三角形三个表的长度.在控制台打印三角形的面积. create or replace procedure pro_s(v_a number,v_b number,v_c number) ...
- 搞清楚MySQL事务隔离级别
首先创建一个表 account.创建表的过程略过(由于 InnoDB 存储引擎支持事务,所以将表的存储引擎设置为 InnoDB).表的结构如下: 然后往表中插入两条数据,插入后结果如下: 为了说明问题 ...
- linux 禁ping
今天用nmap扫描了局域网的主机,发现几个主机开着好多危险端口,做linux的,对这些安全知识有一点了解.遂用nmap扫描了自己的主机是否存在可利用端口.发现每次nmap都能成功的检测我的主机是ali ...
- 发布Rest风格的WebService的SpringBoot极简例子
JDK:1.8.0_212 IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE) 工程下载:https://files.cnblogs.com/file ...
- 理解JVM
1.JVM运行时数据区 2.方法区 方法区垃圾回收的条件:该类的所有实例(堆内存中)被回收:加载该类字节码的类加载器被回收:所有的类对象(如Student.class)的引用被回收 一般采用可达性分析 ...