当前位置:网站首页>High frequency leetcode deep search part: 733 Image rendering
High frequency leetcode deep search part: 733 Image rendering
2022-07-22 09:28:00 【Hiccup~~~~】
733. Image rendering
The difficulty is simple 278
There is a picture in two-dimensional integer array , Each integer represents the pixel value size of the picture , Values in 0 To 65535 Between .
Give you a coordinate (sr, sc) Represents the pixel value at the beginning of image rendering ( That's ok , Column ) And a new color value newColor, Let you re color this image .
In order to finish the coloring work , From the initial coordinates , Record the connected pixel points with the same pixel value in the upper, lower, left and right four directions of the initial coordinate , Then record the qualified pixel points in these four directions and the connected pixel points with the same pixel values and initial coordinates in their corresponding four directions ,……, Repeat the process . Change the color value of all recorded pixels to the new color value .
Finally, return to the color rendered image .
Example 1:
Input :
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output : [[2,2,2],[2,2,0],[2,0,1]]
analysis :
In the middle of the image ,( coordinate (sr,sc)=(1,1)),
The color of all the eligible pixels on the path is changed to 2.
Be careful , The lower right pixel is not changed to 2,
Because it is not a pixel point connected to the initial point in four directions .
Be careful :
- image and image[0] The length of is in the range [1, 50] Inside .
- The initial point given will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
- image[i][j] and newColor The color value is in the range [0, 65535] Inside .
Ideas
- A special case : When newcolor==oldcolor, Go straight back to
Code
class Solution {
public:
int oldColor;
void dfs(vector<vector<int>>& image, int sr, int sc, int newColor){
int n,m;
n=image.size();
m=image[0].size();
if(image[sr][sc]!=oldColor) return;
// Make a mark : Has arrived
image[sr][sc]=newColor;
// Continue searching in four directions
if(sr+1<n) dfs(image,sr+1,sc,newColor);
if(sr-1>=0) dfs(image,sr-1,sc,newColor);
if(sc+1<m) dfs(image,sr,sc+1,newColor);
if(sc-1>=0) dfs(image,sr,sc-1,newColor);
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
oldColor=image[sr][sc];
if(newColor==oldColor) return image;
dfs(image,sr,sc,newColor);
return image;
}
};
边栏推荐
猜你喜欢
随机推荐
Redis原理之BigKey和热点Key
Leetcode1486: array XOR operation
Go死锁问题
EasyCVR平台设备分组新增以及编辑操作异常的问题修复
Chrome插件开发教程
Bigkey and hotspot key of redis principle
Bitmap of redis principle
上市公司环境数据集:环境绩效明细表、排放明细表、资源消耗明细表等多项指标数据
EasyCVR平台安全扫描提示Go pprof调试信息泄露的解决办法
数千万数据量:2000-2019全中国省、市、县企业注册数据经纬度、注册数目等多指标信息
Hyperloglog of redis principle
c语言之指针(二)
嵌入式之网络接口方案介绍与驱动调试方式总结
The difference between delete and truncate in SQL
c语言之指针(一)
异常类
跨境贸易术语
C语言之位操作和整形的补位
h5py快速入门指南
聊天室项目