Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情。
刷题上瘾计划
导读
曾经实施过的计划(在我博客都能搜到):
自从开始记录自己的刷题笔记开始我从每日肥学系列,到动态规划二十一天,再到大厂特训二十八天,还有不攻下dfs不比赛系列。有兴趣大家可以去看看。从最后的结果来看我收到了一些志同道合的朋友一起刷题。如果你也想要加入我们可以直接私信我加入我们也可以下面留言打卡,现在我们的队伍还不是很大但是也有几位成功的同学了,有的进入了不错的公司(当然不能说全靠刷题得来的)有的拿到了比赛证书。接下来我会继续坚持下去,这个“瘾”我们要培养起来。所以我们再来一个为期二十八天刷题上瘾训练计划。等这二十八天过完我们在来一起总结一下。
PS:算法不太分语言大家都可以来的。
题目
有一幅以 m x n 的二维整数数组表示的图画 image ,其中 image[i][j] 表示该图画的像素值大小。
你也被给予三个整数 sr , sc 和 newColor 。你应该从像素 image[sr][sc] 开始对图像进行 上色填充 。
为了完成 上色工作 ,从初始像素开始,记录初始坐标的 上下左右四个方向上 像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应 四个方向上 像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为 newColor 。
最后返回 经过上色渲染后的图像 。
示例 1:
输入: 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]]
复制代码
测试地址:地址
思路我们从给定的起点开始,进行深度优先搜索。每次搜索到一个方格时,如果其与初始位置的方格颜色相同,就将该方格的颜色更新,以防止重复搜索;如果不相同,则进行回溯。
注意:因为初始位置的颜色会被修改,所以我们需要保存初始位置的颜色,以便于之后的更新操作。
解一:
class Solution {
int[] x={0,0,-1,1};//上下左右
int[] y={1,-1,0,0};
public int[][] dfs(int[][]image,int sr,int sc,int newColor,int flag){
if((image[sr][sc]!=flag)||(newColor==flag))return image;
image[sr][sc]=newColor;
for(int i=0;i<4;i++){
int row=x[i]+sr;
int col=y[i]+sc;
if(row<=image.length-1&&row>=0&&col>=0&&col<=image[0].length-1){//判断是否溢出
dfs(image,row,col,newColor,flag);
if(image[row][col]==flag){
image[row][col]=newColor;
}
}
}
return image;
}
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int flag=image[sr][sc];
return dfs(image,sr,sc,newColor,flag);
}
}
复制代码
解二:
class Solution {
int[] dx = {1, 0, 0, -1};
int[] dy = {0, 1, -1, 0};
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int currColor = image[sr][sc];
if (currColor != newColor) {
dfs(image, sr, sc, currColor, newColor);
}
return image;
}
public void dfs(int[][] image, int x, int y, int color, int newColor) {
if (image[x][y] == color) {
image[x][y] = newColor;
for (int i = 0; i < 4; i++) {
int mx = x + dx[i], my = y + dy[i];
if (mx >= 0 && mx < image.length && my >= 0 && my < image[0].length) {
dfs(image, mx, my, color, newColor);
}
}
}
}
}
复制代码
调试
这里给出调试代码: 可以再eclipse或者idea中调试
public class Solution {
int[] x = { 0, 0, -1, 1 };// 上下左右
int[] y = { 1, -1, 0, 0 };
public int[][] dfs(int[][] image, int sr, int sc, int newColor, int flag) {
if ((image[sr][sc] != flag)||(newColor==flag))
return image;
image[sr][sc] = newColor;
for (int i = 0; i < 4; i++) {
int row = x[i] + sr;
int col = y[i] + sc;
if (row <= image.length - 1 && row >= 0 && col >= 0 && col <= image[0].length - 1) {
dfs(image, row, col, newColor, flag);
if (image[row][col] == flag) {
image[row][col] = newColor;
}
}
}
return image;
}
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int flag = image[sr][sc];
return dfs(image, sr, sc, newColor, flag);
}
public static void main(String[] args) {
Solution s=new Solution();
int[][] image= {{0,0,0},{0,1,1}};
image=s.floodFill( image, 1, 1, 1);
for(int[] i:image) {
for(int j:i) {
System.out.print(j+" ");
}
System.out.println();
}
}
}
复制代码
重要的事情
如果你在学习python或者Java哪怕是C遇到问题都可以来给我留言,因为在学习初期新手总会走很多弯路,这个时候如果没有有个人来帮一把的话很容易就放弃了。身边很多这样的例子许多人学着学着就转了专业换了方向,不仅是自身问题还是没有正确的学习。所以作为一个过来人我希望有问题给我留言,说不上是帮助就是顺手敲几行字的事情。