当前位置:网站首页>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;
边栏推荐
猜你喜欢

leetcode303 Weekly Match Replay
![[MySQL] Mysql paradigm and the role of foreign keys](/img/9d/a4295de26683d7bca2b8e9d14f754b.png)
[MySQL] Mysql paradigm and the role of foreign keys

【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发

mongo进入报错

button控件的使用

外媒所言非虚,苹果降价或许是真的在清库存

The new BMW 3 Series is on the market, with safety and comfort

AVH Deployment Practice (1) | Deploying the Flying Paddle Model on Arm Virtual Hardware

radiobutton的使用

"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
随机推荐
【MySQL】Mysql范式及外键作用
ASP.NET Core generates continuous Guid
「秋招系列」MySQL面试核心25问(附答案)
【7.28】代码源 - 【Fence Painting】【合适数对(数据加强版)】
ansible学习笔记02
WPF项目--控件入门基础用法,必知必会XAML
The principle of hough transform detection of straight lines (opencv hough straight line detection)
Tencent Cloud Deployment----DevOps
Linux查看redis版本(查看mongodb版本)
LeetCode_733_图像渲染
工程力学复习资料
tensorflow2.0 cnn(layerwise)
Word table to Excel
更新数据表update
radiobutton的使用
tooltips使用教程(鼠标悬停时显示提示)
WPF project - basic usage of controls entry, you must know XAML
Precautions and solutions when SIGABRT error is reported
工程水文学复习资料
字符指针赋值[通俗易懂]