当前位置:网站首页>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.length
n == image[i].length
1 <= m, n <= 50
0 <= image[i][j], newColor < 2^(16)
0 <= sr < m
0 <= 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;
边栏推荐
猜你喜欢
C程序是如何跑起来的01 —— 普通可执行文件的构成
TRACE32 - SNOOPer-based variable logging
Grafana安装后web打开报错
TextBlock控件入门基础工具使用用法,取上法入门
外媒所言非虚,苹果降价或许是真的在清库存
「秋招系列」MySQL面试核心25问(附答案)
[CUDA study notes] First acquaintance with CUDA
Internet banking stolen?This article tells you how to use online banking safely
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
Qt实战案例(54)——利用QPixmap设计图片透明度
随机推荐
Deployment应用生命周期与Pod健康检查
Replication Latency Case (1) - Eventual Consistency
TRACE32 - C source code association
vb中如何连接mysql_vb怎么连接数据库「建议收藏」
mongo enters error
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
JVM参数解析 Xmx、Xms、Xmn、NewRatio、SurvivorRatio、PermSize、PrintGC「建议收藏」
border控件的使用
What is the difference between BI software in the domestic market?
Doing things software development - the importance of law and understanding of reasonable conclusions
t-sne 数据可视化网络中的部分参数+
使用 GraphiQL 可视化 GraphQL 架构
Snake Project (Simple)
MySQL database operations
Oracle动态注册非1521端口
网银被盗?这篇文章告诉你如何安全使用网银
Insert into data table to insert data
长得很怪的箱图
radiobutton的使用
json到底是什么(c# json)