当前位置:网站首页>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.length
n == image[i].length
1 <= m, n <= 50
0 <= image[i][j], newColor < 2^(16)
0 <= sr < m
0 <= 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;
边栏推荐
- 为什么黑客领域几乎一片男生?
- The R language ggstatsplot package ggbarstats function visualizes bar charts, and adds hypothesis test results (including sample number, statistics, effect size and its confidence interval, significan
- R language test whether the sample conforms to normality (test whether the sample comes from a normally distributed population): shapiro.test function tests whether the sample conforms to the normal d
- 实现防抖与节流函数
- mongo enters error
- 做事软件开发-法的重要性所在以及合理结论的认识
- LeetCode_733_图像渲染
- mysql黑窗口~建库建表
- 基于ABP实现DDD
- Kubernetes原理剖析与实战应用手册,太全了
猜你喜欢
6-22 Vulnerability exploit - postgresql database password cracking
工程流体力学复习
MySQL基础篇【单行函数】
WPF project - basic usage of controls entry, you must know XAML
Public Key Retrieval is not allowed error solution when DBeaver connects to MySQL 8.x
Getting Started with TextBlock Control Basic Tools Usage, Get Started
第二届中国PWA开发者日
mongo enters error
tooltips使用教程(鼠标悬停时显示提示)
What is the difference between BI software in the domestic market?
随机推荐
"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
After Grafana is installed, the web opens and reports an error
Kubernetes常用命令
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
SQL、HQL、JPQL 到底有什么区别
Implementing click on the 3D model in RenderTexture in Unity
What is the difference between BI software in the domestic market?
Delete the disk in good condition (recovery partition)
BGP综合实验(建立对等体、路由反射器、联邦、路由宣告及聚合)
leetcode303 Weekly Match Replay
自动化测试如何创造业务价值?
R language moves time series data forward or backward (custom lag or lead period): use the lag function in the dplyr package to move the time series data forward by one day (set the parameter n to a p
The principle of hough transform detection of straight lines (opencv hough straight line detection)
Matlab matrix basic operations (definition, operation)
腾讯云部署----DevOps
Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
AVH Deployment Practice (1) | Deploying the Flying Paddle Model on Arm Virtual Hardware
6-22漏洞利用-postgresql数据库密码破解
Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发