博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 733. Flood Fill
阅读量:4090 次
发布时间:2019-05-25

本文共 1020 字,大约阅读时间需要 3 分钟。

 

一、问题描述

给定一个二维数组以及一个坐标,将其置为新的值,并且将由该坐标可到达的、与其值相同的所有元素重置为新的值。

 

测试用例:

Input:image = [[1,1,1],[1,1,0],[1,0,1]]sr = 1, sc = 1, newColor = 2Output:[[2,2,2],[2,2,0],[2,0,1]]

 

二、代码实现

class Solution {    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {        if (image == null || image.length == 0) {            return image;        }        int oldColor = image[sr][sc];        if (oldColor != newColor) {            dfs(image, sr, sc, oldColor, newColor);        }        return image;    }    private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {        if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] != oldColor             || oldColor == newColor) {                return;        }        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);   //右    }}

 

转载地址:http://afnii.baihongyu.com/

你可能感兴趣的文章
通过Spring Boot三分钟创建Spring Web项目
查看>>
Spring的IoC(依赖注入)原理
查看>>
Guava快速入门
查看>>
Java编程基础:static的用法
查看>>
Java编程基础:抽象类和接口
查看>>
Java编程基础:异常处理
查看>>
Java编程基础:了解面向对象
查看>>
新一代Java模板引擎Thymeleaf
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
Spring Boot构建简单的微博应用
查看>>
Spring处理表单提交
查看>>
Spring MVC异常处理
查看>>
Leetcode 1180. Count Substrings with Only One Distinct Letter [Python]
查看>>
PHP 7 的五大新特性
查看>>
php使用 memcache 来存储 session
查看>>
php实现socket(转)
查看>>
PHP底层的运行机制与原理
查看>>
php 几个比较实用的函数
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>