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的油漆桶功能.算法的原理很简单,就 ...
随机推荐
- day31-python阶段性复习五
打印目录下所有文件 os 模块 os.listdir(‘/home’) 列出目录下所有文件 os.path.isdir(‘/home’) 判断一个文件是不是一个目录 os.path.isfile(‘/ ...
- OOP⑶
/** * 学生类 * ctrl+o 查询本类的所有属性 和方法 */ public class Student { int age; // 年龄 String name; // 姓名 // 自己创建 ...
- Java四个常用正则表达
1.查询 以下是代码片段: String str="abc efg ABC";String regEx="a|f"; //表示a或fPattern p=P ...
- C++构造函数和析构函数,以及构造函数特殊成员变量和函数的初始化
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 使用libcurl下载https地址的文件
使用libcurl下载https地址的文件 void downLoadFile(std::string filename, std::string newFilename) { CURL *curl_ ...
- JAVA面向对象和类
一.构造方法 1.构造方法是一个与类同名的方法,用来对类进行实例化(创建对象) 2.特点 1).构造方法没有返回值: 2).构造方法的名称要与本类的名称相同. 例 public class Test{ ...
- e与复利
e≍2.718 计算一个复利例子,设本金p,年利率为r,每月计算一次利息,月利息为r/12,则一年的本息一共: p(1+r/12)12=pq 当计算复利的时间间隔越来越小,根据上面极限公式,本金所乘的 ...
- pprof进行golang程序性能分析
一.导入包 import _ "net/http/pprof" 二.启动监控routine go func() { http.ListenAndServe("0.0.0. ...
- 【Leetcode】292. Nim Game
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- ZK集群搭建和配置
1. 克隆三台空虚拟机(含有jdk和lrzsz),修改网络ip,并关闭虚拟机的防火墙 临时关闭:service iptables stop 永久关闭:chkconfig iptables off 2. ...