ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

面试题 08.10 颜色填充(java)(dfs)

2020-03-09 19:09:23  阅读:252  来源: 互联网

标签:面试题 newColor int sr image 08.10 dfs sc


颜色填充。编写函数,实现许多图片编辑软件都支持的“颜色填充”功能。给定一个屏幕(以二维数组表示,元素为颜色值)、一个点和一个新的颜色值,将新颜色值填入这个点的周围区域,直到原来的颜色值全都改变。

示例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,
因为它不是在上下左右四个方向上与初始点相连的像素点。
说明:

image 和 image[0] 的长度在范围 [1, 50] 内。
给出的初始点将满足 0 <= sr < image.length 和 0 <= sc < image[0].length。
image[i][j] 和 newColor 表示的颜色值在范围 [0, 65535]内。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/color-fill-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int x = image.length;
        int y = image[0].length;
        if((sr < 0 || sr >= x) || (sc < 0 || sc >= y) || (image[sr][sc] == newColor)){
            return image;
        }  
        dfs(image, sr, sc, image[sr][sc], newColor);
        return image;
    }
    public void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
        int x = image.length;
        int y = image[0].length;
        if((sr < 0 || sr >= x) || (sc < 0 || sc >= y)){
            return;
        }      
        if(image[sr][sc] == oldColor) {
            image[sr][sc] = newColor;
            dfs(image, sr-1, sc, oldColor, newColor);
            dfs(image, sr+1, sc, oldColor, newColor);
            dfs(image, sr, sc-1, oldColor, newColor);
            dfs(image, sr, sc+1, oldColor, newColor);
        }
    }
}

标签:面试题,newColor,int,sr,image,08.10,dfs,sc
来源: https://blog.csdn.net/weixin_43306331/article/details/104758799

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有