LeetCode - 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.
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].
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if(image == null){
return null;
}
if(newColor == image[sr][sc]){
return image;
}
//int[][] res = new int[image.length][image[0].length];
int origin = image[sr][sc];
image[sr][sc] = newColor;
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{sr, sc}); while(!queue.isEmpty()){
Queue<int[]> temp = new LinkedList<>();
for(int[] index : queue){
int x = index[0];
int y = index[1];
//move up
if(x - 1 >= 0 && image[x - 1][y] == origin){
image[x - 1][y] = newColor;
temp.add(new int[]{x-1,y});
} //move down
if(x + 1 < image.length && image[x +1][y] == origin){
image[x + 1][y] = newColor;
temp.add(new int[]{x+1, y});
}
//move left
if(y - 1 >= 0 && image[x][y - 1] == origin){
image[x][y - 1] = newColor;
temp.add(new int[]{x, y-1});
}
//move right
if(y + 1 < image[0].length && image[x][y + 1] == origin){
image[x][y + 1] = newColor;
temp.add(new int[]{x, y+1});
}
}
queue = temp;
}
return image;
}
}
LeetCode - Flood Fill的更多相关文章
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)
Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...
- 【LeetCode】733. Flood Fill 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 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 ...
- 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 ...
- [Swift]LeetCode733. 图像渲染 | 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的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
随机推荐
- unity3D打包发布Apk详细步骤
1.复制android-sdk-windows文件夹到C盘或者D盘或者你可以找到的任意盘任意目录,注意:不能在中文目录下!! 复制完成之后,打开unity,新建一个项目,打开Edit-Preferen ...
- python -input用户输入
#接收用户输入信息用input就可以了 #还有输入密码的,也就是隐藏的,pycharm中不好用,要到命令行去 import getpass name = input('name:') age = in ...
- 微信小程序图表插件 - wx-charts
微信小程序图表插件(wx-charts)基于canvas绘制,体积小巧支持图表类型饼图.线图.柱状图 .区域图等图表图形绘制,目前wx-charts是微信小程序图表插件中比较强大好使的一个. wx-c ...
- VSTO杂项拾零(持续更新中……)
环境:win 7+visual basic 2008 侧重:VSTO 界面:sheetbook工作簿 1.创建一个过程并调用(2017.6.3) Public Class Sheet1 ...
- java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法
今天在写一个JAVA程序的时候出现了异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact repr ...
- mybatis动态sql #和$的区别
$和#都支持动态sql:就是你传什么它就是什么 区别: 1.#可以防止sql注入在sql执行时显示 '?' 比$安全 SELECT * FROM table WHERE id = ? 2.在使用#传入 ...
- jmeter中添加压力机
在压测的时候,可能并发比较大,一台机子已经启动不了那么多并发了,这个时候就是有多台机子一起来并发,就要添加压力机 如何添加压力机呢: 1.其他电脑上也安装了jmeter,和其他电脑都能ping通当前电 ...
- HTTP网页过程
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: (1) 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立 ...
- MySQL:常用的数据模型
常用的数据模型 一.定义 数据模型是对现实世界数据特征的抽象:通俗的讲数据模型就是现实世界的模拟: 数据模型是严格定义的一组概念的集合 是用来抽象.表示和处理现实世界中的数据和信息的工具 是对现实世 ...
- css 实现省略号. text-overflow: ellipsis; 同时设置四个属性才可以.
这个同时需要. text-overflow ; overflow ; white-space ; width ; 四个属性才可以. <!DOCTYPE html> <htm ...