题目:

有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newColor,让你重新上色这幅图像。

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.

示例 1:

输入:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
输出: [[2,2,2],[2,2,0],[2,0,1]]
解析:
在图像的正中间,(坐标(sr,sc)=(1,1)),
在路径上所有符合条件的像素点的颜色都被更改成2。
注意,右下角的像素没有更改为2,
因为它不是在上下左右四个方向上与初始点相连的像素点。

注意:

  • imageimage[0] 的长度在范围 [1, 50] 内。
  • 给出的初始点将满足 0 <= sr < image.length0 <= sc < image[0].length
  • image[i][j]newColor 表示的颜色值在范围 [0, 65535]内。

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

解题思路:

​ 与01矩阵类似,在图的数据结构内找到所有旧的像素点改成新的新素值。无非是图的遍历,BFS和DFS。

就这道题而言,不涉及路径长度,明显DFS深度优先遍历更适合。因为BFS广度优先遍历需要记录每个相邻符合要求的位置,并且不能添加重复的点。 DFS可以用栈或递归实现,如果用栈来解虽然比递归更好理解一些,但是每次依然要存储每个点的索引位置,并且出入栈也会消耗时间。所以这道题的最优解应该是用递归实现的深度优先遍历解题。

代码:

DFS(Java):

class Solution {
private boolean withinBounds(int[][] img, int i, int j) {//判断指针是否溢出
return (i < img.length && i >= 0) && (j < img[0].length && j >= 0);
} private void floodFillProcess(int[][] img, int sr, int sc, int oc, int nc) {
if (withinBounds(img, sr, sc) && img[sr][sc] == oc) {//指针不溢出且像素值为旧值时
img[sr][sc] = nc;//改为新值
floodFillProcess(img, sr - 1, sc, oc, nc);//递归上下左右四个点
floodFillProcess(img, sr + 1, sc, oc, nc);
floodFillProcess(img, sr, sc - 1, oc, nc);
floodFillProcess(img, sr, sc + 1, oc, nc);
}
} public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int oc = image[sr][sc];
if (newColor == oc) return image;
floodFillProcess(image, sr, sc, oc, newColor);
return image;
}
}

DFS(Python):

class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
oldColor = image[sr][sc]
if oldColor == newColor:
return image
self.dfs(image, sr, sc, oldColor, newColor)
return image def dfs(self, image: List[List[int]], sr: int, sc: int, oldColor: int, newColor: int):
if image[sr][sc] == oldColor:
image[sr][sc] = newColor
if sr-1 >= 0:#先判断是否溢出再决定是否递归
self.dfs(image, sr-1, sc, oldColor, newColor)
if sr+1 < len(image):
self.dfs(image, sr+1, sc, oldColor, newColor)
if sc-1 >= 0:
self.dfs(image, sr, sc-1, oldColor, newColor)
if sc+1 < len(image[0]):
self.dfs(image, sr, sc+1, oldColor, newColor)

附:

BFS深度优先遍历(Java):

class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int oldColor = image[sr][sc];
if (oldColor == newColor) return image;//旧像素值与新像素值相等时,无需修改
int rows = image.length;
int columns = image[0].length;
bfs(image, sr * columns + sc, rows, columns, newColor, oldColor);//进入BFS辅助函数
return image;
} private void bfs(int[][] img, int loc, int row, int column, int nc, int oc) {
Set<Integer> set = new LinkedHashSet<>(); //set(),避免添加重复点
Queue<Integer> queue = new LinkedList<>();
queue.add(loc);//队列加入第一个初始点,记录点索引的方式是x*column+y,
while (!queue.isEmpty()) {
int tmp = queue.poll();
int r = tmp / column, c = tmp % column;//拆解位置
if (img[r][c] == oc && !set.contains(tmp)) {//像素值为旧值,并且该点未被计算过
img[r][c] = nc;//改为新值
set.add(tmp);
if (r + 1 < row) if (img[r + 1][c] == oc) queue.add((r + 1) * column + c);
if (r - 1 >= 0) if (img[r - 1][c] == oc) queue.add((r - 1) * column + c);
if (c + 1 < column) if (img[r][c + 1] == oc) queue.add(r * column + c + 1);
if (c - 1 >= 0) if (img[r][c - 1] == oc) queue.add(r * column + c - 1);
}
}
}
}

LeetCode 733: 图像渲染 flood-fill的更多相关文章

  1. Java实现 LeetCode 733 图像渲染(DFS)

    733. 图像渲染 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的 ...

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

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

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

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

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

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

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

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

  7. Leetcode733.Flood Fill图像渲染

    有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newCol ...

  8. [LeetCode] Flood Fill 洪水填充

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

  9. 【Leetcode_easy】733. Flood Fill

    problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...

随机推荐

  1. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

  2. IT兄弟连 Java语法教程 流程控制语句 循环结构语句1

    循环语句可以在满足循环条件的情况下,反复执行某一点代码,这段被重复执行的代码被称为循环体,当反复执行这个循环体时,需要在合适的时候把循环条件该为假,从而结束循环,否则循环将一直执行下去,形成死循环.循 ...

  3. 【shell脚本】自动磁盘分区,格式化,挂载===autoMount.sh

    #!/bin/bash # 自动对磁盘分区.格式化.挂载 # 对虚拟机的 vdb 磁盘进行分区格式化,使用<<将需要的分区指令导入给程序 fdisk # n(新建分区),p(创建主分区), ...

  4. 08-蓝图&单元测试

    学习目标 能够使用代码实现蓝图对项目进行模块化 能够说出断言的作用 能够说出实现单元测试步骤 能够说出单元测试所执行方法的定义规则 Blueprint(蓝图) 随着flask程序越来越复杂,我们需要对 ...

  5. 害死人不偿命的(3n+1)猜想-PTA

    卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n+1) 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n=1.卡拉兹在 1950 ...

  6. pandas 学习 第6篇:DataFrame - 数据处理(长宽格式、透视表)

    长宽格式的转换 宽格式是指:一列或多列作为标识变量(id_vars),其他变量作为度量变量(value_vars),直观上看,这种格式的数据比较宽,举个列子,列名是:id1.id2.var1.var2 ...

  7. ABAP 新语法记录(一)

    原文链接:https://www.cnblogs.com/learnning/p/10647174.html 主要内容 内联声明 构造表达式 内表操作 Open SQL 其他 本文列出了ABAP新语法 ...

  8. Java反射及注解

    一.反射 1.动态语言:是指程序在运行是可以改变其结构:新的函数可以引进,已有的函数可以被删除等结构上的变化.比如常见的JavaScript就是动态语言,除此以外Python等也属于动态语言,而C.C ...

  9. SAP MM MB5L事务代码'仅总计'选项初探

    SAP MM MB5L事务代码'仅总计'选项初探 MB5L,如下查询条件, 报表结果里显示有差异, 而如下查询条件, 原因在于当勾选了'仅总计'选项以后,系统不考虑MM以外的影响库存金额的事务,而只是 ...

  10. AFNetworking遇到错误 Request failed: unacceptable content-type: text/html

    iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html 原因: 不可接受的内容类型 “text/html ...