当前位置:网站首页>LeetCode_733_图像渲染
LeetCode_733_图像渲染
2022-07-31 15:46:00 【Fitz1318】
题目链接
题目描述
有一幅以 m x n 的二维整数数组表示的图画 image ,其中 image[i][j] 表示该图画的像素值大小。
你也被给予三个整数 sr , sc 和 newColor 。你应该从像素 image[sr][sc] 开始对图像进行 上色填充 。
为了完成 上色工作 ,从初始像素开始,记录初始坐标的 上下左右****四个方向上 像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应 四个方向上 像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为 newColor 。
最后返回 经过上色渲染后的图像 。
示例 1:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-StSNbfHJ-1659236734341)(https://assets.leetcode.com/uploads/2021/06/01/flood1-grid.jpg)]
输入: 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,因为它不是在上下左右四个方向上与初始点相连的像素点。
示例 2:
输入: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2
输出: [[2,2,2],[2,2,2]]
提示:
m == image.lengthn == image[i].length1 <= m, n <= 500 <= image[i][j], newColor < 2^(16)0 <= sr < m0 <= sc < n
解题思路
- 记录下当前点的原像素值
- 当前点的像素值与
newColor相同则直接返回,否则进入下一步 - 将当前像素值改成
newColor - 对当前点的上下左右四个方向上满足以下条件的点进行递归
- 坐标不超数组长度
- 像素值等于当前点的像素值
AC代码
int curColor = image[sr][sc];//记录当前点的像素值
if (curColor == color) {
return image;
}
image[sr][sc] = color;
//左
if (sr - 1 >= 0 && image[sr - 1][sc] == curColor) {
floodFill(image, sr - 1, sc, color);
}
//右
if (sr + 1 < image.length && image[sr + 1][sc] == curColor) {
floodFill(image, sr + 1, sc, color);
}
//上
if (sc + 1 < image[0].length && image[sr][sc + 1] == curColor) {
floodFill(image, sr, sc + 1, color);
}
//下
if (sc - 1 >= 0 && image[sr][sc - 1] == curColor) {
floodFill(image, sr, sc - 1, color);
}
return image;
边栏推荐
- Kubernetes常用命令
- Applicable scenario of multi-master replication (2) - client and collaborative editing that require offline operation
- 自动化测试如何创造业务价值?
- Efficient use of RecyclerView Section 3
- 多主复制的适用场景(1)-多IDC
- How does automated testing create business value?
- ML.NET related resources
- Replication Latency Case (1) - Eventual Consistency
- C语言”三子棋“升级版(模式选择+AI下棋)
- mysql黑窗口~建库建表
猜你喜欢
随机推荐
Qt实战案例(54)——利用QPixmap设计图片透明度
What is the difference between BI software in the domestic market?
Kubernetes原理剖析与实战应用手册,太全了
Gorm—Go language database framework
[CUDA study notes] First acquaintance with CUDA
leetcode303 Weekly Match Replay
WPF project - basic usage of controls entry, you must know XAML
org.apache.jasperException(could not initialize class org)
After Grafana is installed, the web opens and reports an error
ASP.NET Core generates continuous Guid
R language ggplot2 visualization: use the ggboxplot function of the ggpubr package to visualize the grouped box plot, use the ggpar function to change the graphical parameters (caption, add, modify th
Browser's built-in color picker
TextBlock控件入门基础工具使用用法,取上法入门
Efficient use of RecyclerView Section 2
Handling write conflicts under multi-master replication (4) - multi-master replication topology
TRACE32 - SNOOPer-based variable logging
The use of border controls
C语言”三子棋“升级版(模式选择+AI下棋)
6-22漏洞利用-postgresql数据库密码破解
hough变换检测直线原理(opencv霍夫直线检测)









