当前位置:网站首页>LeetCode_733_Image rendering
LeetCode_733_Image rendering
2022-07-31 15:49:00 【Fitz1318】
题目链接
题目描述
有一幅以 m x n 的二维整数数组表示的图画 image ,其中 image[i][j] 表示该图画的像素值大小.
你也被给予三个整数 sr , sc 和 newColor .你应该从像素 image[sr][sc] 开始对图像进行 上色填充 .
为了完成 上色工作 ,从初始像素开始,记录初始坐标的 上下左右****四个方向上 像素值与初始坐标相同的相连像素点,Then record this四个方向上Eligible pixels correspond to them 四个方向上 像素值与初始坐标相同的相连像素点,……,重复该过程.将所有有记录的像素点的颜色值改为 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
解题思路
- Record the original pixel value of the current point
- The pixel value of the current point and
newColor相同则直接返回,否则进入下一步 - Change the current pixel value to
newColor - Perform recursion on the points that satisfy the following conditions in the four directions of the current point, up, down, left, and right
- The coordinates do not exceed the array length
- The pixel value is equal to the pixel value of the current point
AC代码
int curColor = image[sr][sc];//Record the pixel value of the current point
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;
边栏推荐
- Linux check redis version (check mongodb version)
- hough变换检测直线原理(opencv霍夫直线检测)
- 字符串反转的实现方法总结「建议收藏」
- 全新宝马3系上市,安全、舒适一个不落
- OPPO在FaaS领域的探索与思考
- Why don't you make a confession during the graduation season?
- 【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
- what exactly is json (c# json)
- .NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
- 贪吃蛇项目(简单)
猜你喜欢
随机推荐
Handling write conflicts under multi-master replication (4) - multi-master replication topology
百度网盘网页版加速播放(有可用的网站吗)
【7.28】代码源 - 【Fence Painting】【合适数对(数据加强版)】
浏览器自带的拾色器
Implement anti-shake and throttling functions
WPF项目--控件入门基础用法,必知必会XAML
The use of button controls
数据库的范式(第一范式,第二范式,第三范式,BCNF范式)「建议收藏」
7、常见面试口语提问问题汇总
国内市场上的BI软件,到底有啥区别
Qt practical cases (54) - using transparency QPixmap design pictures
工程水文学名词解释总结
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
字符指针赋值[通俗易懂]
多主复制的适用场景(1)-多IDC
长得很怪的箱图
vb中如何连接mysql_vb怎么连接数据库「建议收藏」
JVM parameter analysis Xmx, Xms, Xmn, NewRatio, SurvivorRatio, PermSize, PrintGC "recommended collection"
TRACE32 - C source code association
ML.NET相关资源整理








