首页 > 其他分享 >leetcode-733-easy

leetcode-733-easy

时间:2023-03-31 20:14:58浏览次数:35  
标签:int sr image pixel color 733 easy sc leetcode

Flood Fill

An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.
You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color 
as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. 
Replace the color of all of the aforementioned pixels with color.
Return the modified image after performing the flood fill.

Example 1:

Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
Example 2:

Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
Explanation: The starting pixel is already colored 0, so no changes are made to the image.
Constraints:

m == image.length
n == image[i].length
1 <= m, n <= 50
0 <= image[i][j], color < 216
0 <= sr < m
0 <= sc < n

思路一:用一个数组记录访问过的坐标,递归遍历所有坐标

    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int m = image.length;
        int n = image[0].length;
        int[][] find = new int[m][n];

        fill(image, find, sr, sc, color, image[sr][sc]);

        return image;
    }

    public void fill(int[][] image, int[][] find, int sr, int sc, int color, int oriColor) {
        if (find[sr][sc] == 1) {
            return;
        }

        image[sr][sc] = color;
        find[sr][sc] = 1;

        if (sr - 1 >= 0 && image[sr - 1][sc] == oriColor) {
            fill(image, find, sr - 1, sc, color, oriColor);
        }

        if (sr + 1 <= image.length - 1 && image[sr + 1][sc] == oriColor) {
            fill(image, find, sr + 1, sc, color, oriColor);
        }

        if (sc - 1 >= 0 && image[sr][sc - 1] == oriColor) {
            fill(image, find, sr, sc - 1, color, oriColor);
        }

        if (sc + 1 <= image[0].length - 1 && image[sr][sc + 1] == oriColor) {
            fill(image, find, sr, sc + 1, color, oriColor);
        }
    }

标签:int,sr,image,pixel,color,733,easy,sc,leetcode
From: https://www.cnblogs.com/iyiluo/p/17277347.html

相关文章

  • leetcode-724-easy
    FindPivotIndexGivenanarrayofintegersnums,calculatethepivotindexofthisarray.Thepivotindexistheindexwherethesumofallthenumbersstrictlytotheleftoftheindexisequaltothesumofallthenumbersstrictlytotheindex's......
  • leetcode-744-easy
    FindSmallestLetterGreaterThanTargetYouaregivenanarrayofcharacterslettersthatissortedinnon-decreasingorder,andacharactertarget.Thereareatleasttwodifferentcharactersinletters.Returnthesmallestcharacterinlettersthatis......
  • leetcode-762-easy
    PrimeNumberofSetBitsinBinaryRepresentationGiventwointegersleftandright,returnthecountofnumbersintheinclusiverange[left,right]havingaprimenumberofsetbitsintheirbinaryrepresentation.Recallthatthenumberofsetbitsan......
  • Leetcode Practice --- 栈和队列
    目录155.最小栈思路解析20.有效的括号思路解析1047.删除字符串中的所有相邻重复项思路解析1209.删除字符串中的所有相邻重复项II思路解析删除字符串中出现次数>=2次的相邻字符剑指Offer09.用两个栈实现队列239.滑动窗口最大值思路解析155.最小栈设计一个支持push......
  • LeetCode 100 相同的树
    LeetCode|100.相同的树给你两棵二叉树的根节点p和q,编写一个函数来检验这两棵树是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。示例1:输入:p=[1,2,3],q=[1,2,3]输出:true示例2:输入:p=[1,2],q=[1,null,2]输出:false示例3:输入:p......
  • LeetCode 94 二叉树的中序遍历
    LeetCode|94.二叉树的中序遍历给定一个二叉树的根节点root,返回它的中序 遍历。示例1:输入:root=[1,null,2,3]输出:[1,3,2]示例2:输入:root=[]输出:[]示例3:输入:root=[1]输出:[1]提示:树中节点数目在范围[0,100]内-100<=Node.val<=100迭代实现:......
  • 双网卡设备通过HIKSDK接入EasyCVR平台显示离线是什么原因?
    EasyCVR视频融合平台基于云边端协同架构,具有强大的数据接入、处理及分发能力,平台支持海量视频汇聚管理,可支持多协议接入,包括市场主流标准协议与厂家私有协议及SDK,如:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、宇视SDK等(具体见下图)。平台能在复杂的网络环境中,将分散的各......
  • 华为NVR设备接入EasyCVR视频融合平台后不显示摄像头的问题排查与解决
    在上期的文章中,我们和大家分享了关于EasyCVR平台与华为IVS3800平台的对接相关经验分享,感兴趣的用户可以翻阅我们往期的文章进行查看。今天我们来分享一下华为NVR设备接入平台后不显示摄像头的问题排查与解决。在EasyCVR对接华为NVR设备的过程中,通常是使用国标GB28181协议,但是有......
  • AI视频智能分析平台EasyCVR设备录像功能细节优化
    EasyCVR视频融合平台基于云边端一体化架构,具有强大的数据接入、处理及分发能力,平台支持海量视频汇聚管理,可支持多协议接入,包括市场主流标准协议与厂家私有协议及SDK,如:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、宇视SDK等(具体见下图)。平台丰富的视频能力包括:视频监控直......
  • 202031607332-阿卜杜热合曼·麦麦提艾萨 实验一 软件工程准备—对课程的初步认识
    项目内容班级博客链接2023年春软件工程(2020级计算机科学与技术本次作业要求链接实验一软件工程准备我的课程学习目标1.学习博客园软件开发者学习社区使用技巧和经验2.了解Github的基本操作本次作业在哪些方面帮我实现学习目标学习了博客园使用技巧,Github的......