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的更多相关文章

  1. [LeetCode] Flood Fill 洪水填充

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

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

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

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

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

  4. LeetCode刷题 Flood Fill 洪水填充问题

    An  image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...

  5. [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 ...

  6. 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 ...

  7. [Swift]LeetCode733. 图像渲染 | Flood Fill

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

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

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

  9. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

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

随机推荐

  1. flask下载zip文件报错TypeError

    报错内容:TypeError: make_conditional() got an unexpected keyword argument 'accept_ranges' 报错行自己代码如下: dir ...

  2. 使用ADO.NET访问、查询和操作数据库

    ADO.ENT的主要组件 提供两个组件,用来访问和处理数据:.NET Framework 数据程序和DataSet(数据集) .NET Framework:是专门为数据处理及快速地只进,只读访问数据而 ...

  3. java中方法内可以调用同一个类中的方法

    在同一个类中,java的普通方法的相互调用,可以使用this+点号+方法名,也可省略this+点号,java编 译器会自动补上.

  4. Windows server 2016 安装 TFS

    一:准备: 1.1下载TFS https://visualstudio.microsoft.com/zh-hans/tfs/ 1.2 下载SQL2017 http://msdn.itellyou.cn ...

  5. WebView加载页面

    //使用内置浏览器webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoa ...

  6. Java基础复习

    java语言的一个核心:jdk, java development kits---面向开发人员jre, java Runtime Environment---服务器上 java虚拟机---(以字节码为 ...

  7. SharePoint Framework 把你的客户端web部件连接到SharePoint

    博客地址:http://blog.csdn.net/FoxDave 把你的web部件连接到SharePoint来访问SharePoint中的功能和数据,为终端用户提供更完整的体验.本篇会基于之前构 ...

  8. 转--HC05-两个蓝牙模块间的通信

    示例蓝牙: 蓝牙A地址:3014:10:271614 蓝牙B地址:2015:2:120758 //============================================= 步骤: 1 ...

  9. shell脚本实例-判断主机存活 以及企业备份方案

    1.上次写了一个脚本我那次考虑不是很周全,这次我将脚本改动了一下,这次是判断三次, 希望关注我的人可以经常交流哈.下面我写上代码. #!/usr/bin/bash while read ip do f ...

  10. 24点小游戏app宣传文案

    24点小游戏app宣传文案 游戏背景 24点小游戏是传统的扑克牌游戏,是通过扑克牌来完成的竞争性智力游戏,除了希望能够消磨我们的空闲时间,加强同学们的临机和速算能力,还能够促进我们每个人的大脑和逻辑性 ...