力扣面试题 08.10. 颜色填充(java DFS解法)

news/2024/7/20 22:46:49 标签: 深度优先, leetcode, java

Problem: 面试题 08.10. 颜色填充

文章目录

  • 题目描述
  • 思路
  • 解题方法
  • 复杂度
  • Code

题目描述

在这里插入图片描述在这里插入图片描述

思路

该问题可以归纳为一类遍历二维矩阵的题目,此类中的一部分题目可以利用DFS来解决,具体到本题目:

1.我们从题目给定点处开始依次以上下左右的顺序依次判断是否DFS,具体到代码实现中,我们可以用一个二维数组int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};记录每一个点的四个方位,在开始遍历同时判断;
2.我们在循环中每次得到一个新的点的横纵坐标并判断其是否合法,具体到代码中我们要求
新得到的横纵坐标不超过给定二维数组的索引范围,同时新坐标的值不等给定位置的值、新坐标的值等于给定待修改的值(newCol)

解题方法

1.调用dfs函数,返回image
2.dfs函数:

2.1 每次将当前得到的合法位置修改成给定的值(newCol)
2.2 用一个二维数组int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};记录每一个点的四个方位
2.3 for循环开始查找遍历(循环范围1-4),每次得到一个新的坐标(int newI = sr + directions[i][0];int newJ = sc + directions[i][1];
2.4 每次对新的坐标判断,若
新得到的横纵坐标不超过给定二维数组的索引范围,同时新坐标的值不等给定位置的值、新坐标的值等于给定待修改的值(newCol)则DFS,否则继续循环查找下一个合法
的位置

复杂度

时间复杂度:

O ( m n ) O(mn) O(mn)

空间复杂度:

O ( m n ) O(mn) O(mn)

Code

java">class Solution {
    /**
     * Sets all numbers of the location connected to the specified location to the given number
     *
     * @param image    Given two-dimensional array
     * @param sr       The abscissa of a given position
     * @param sc       The ordinate of a given position
     * @param newColor Given number
     * @return int[][]
     */
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int row = image.length;
        int col = image[0].length;
        dfs(image, row, col, sr, sc, image[sr][sc], newColor);
        return image;
    }

    /**
     * Sets all numbers of a location connected to a specified location to a given number by DFS
     *
     * @param image    Given two-dimensional array
     * @param row      The number of rows of a given matrix
     * @param col      The number of columns  of a given matrix
     * @param sr       The abscissa of a given position
     * @param sc       The ordinate of a given position
     * @param color    The value of the original given position
     * @param newColor Given number
     */
    private void dfs(int[][] image, int row, int col, int sr, int sc, int color, int newColor) {
        //Change the current position to newColor
        image[sr][sc] = newColor;
        //Recode the directions
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (int i = 0; i < 4; ++i) {
            int newI = sr + directions[i][0];
            int newJ = sc + directions[i][1];
            if (newI < 0 || newI >= row || newJ < 0 || newJ >= col
                    || image[newI][newJ] != color || image[newI][newJ] == newColor) {
                continue;
            }
            dfs(image, row, col, newI, newJ, color, newColor);
        }
    }
}

http://www.niftyadmin.cn/n/5273594.html

相关文章

百分比组件 - elementui改动

<el-slider v-model"value2" style"width: 87%;position: absolute;bottom: 9px;" disabled :show-tooltip"false"></el-slider>value2: 0,// 百分比条 ::v-deep .el-slider__runway.disabled .el-slider__bar {background-color: #…

RHEL8_Linux_Ansible常用模块的使用

本章主要介绍Ansible中最常见模块的使用 shell模块文件管理模块软件包管理模块服务管理模块磁盘管理模块用户管理模块防火墙管理模块 ansible的基本用法如下。 ansible 机器名 -m 模块x -a "模块的参数" 对被管理机器执行不同的操作&#xff0c;只需要调用不同的模块…

Ansible:模块1

Ansible&#xff1a; 远程操作主机功能 自动化运维&#xff08;playbook 剧本 yaml&#xff09; 是基于python开发的配置管理和应用部署工具。在自动化运维中&#xff0c;现在是一军突起。 Ansible能批量配置&#xff0c;部署&#xff0c;管理上千台主机。类似于xshell的一…

UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符

文章目录 枚举&#xff08;ENUM&#xff09;第一种方式第二种方式 结构体&#xff08;USTRUCT&#xff09;补充属性说明符&#xff08;ExposeOnSoawn&#xff09;结构体创建数据表格 枚举&#xff08;ENUM&#xff09; 第一种方式 定义枚举 UENUM(BlueprintType) namespace …

(PC+WAP)装修设计公司网站模板 家装公司网站源码下载

(PCWAP)装修设计公司网站模板 家装公司网站源码下载 PbootCMS内核开发的网站模板&#xff0c;该模板适用于装修设计、家装公司类等企业&#xff0c;当然其他行业也可以做&#xff0c;只需要把文字图片换成其他行业的即可&#xff1b; PCWAP&#xff0c;同一个后台&#xff0c…

Caught exception in launch(see debug for traceback)

Caught exception in launch (see debug for traceback): Caught exception when trying to load file of format [xml]: Caught exception when trying to load file of format [xml]: junk after document element: line 8, column 9 罪魁祸首&#xff1a; 看吧我在launch文件…

文字识别和阅读软件OmniReader Pro mac介绍

OmniReader Pro mac是一款文字识别和阅读软件&#xff0c;它可以将印刷体和手写体的文字转换为数字文本&#xff0c;并将其朗读出来。该软件适用于视力受损、阅读困难、语言障碍等用户&#xff0c;可以帮助他们更加轻松地获取信息和阅读文本。 OmniReader Pro mac具有简洁直观的…

姿态识别、目标检测和跟踪的综合应用

引言&#xff1a; 近年来&#xff0c;随着人工智能技术的不断发展&#xff0c;姿态识别、目标检测和跟踪成为了计算机视觉领域的热门研究方向。这三个技术的综合应用为各个行业带来了巨大的变革和机遇。本文将分别介绍姿态识别、目标检测和跟踪的基本概念和算法&#xff0c;并探…