作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/flood-fill/description/

题目描述

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, “flood fill” the image.

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 as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.

At the end, return the modified image.

Example 1:
Input:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]] Explanation:
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
by a path of the same color as the starting pixel 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.

Note:

  1. The length of image and image[0] will be in the range [1, 50].
  2. The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < 1. image[0].length.
  3. The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

题目大意

简而言之:对指定位置染色,染色后,再对与该染色点四个方向相邻的相同颜色的点进行染色,依次递归。

图像由二维整数数组表示,每个整数代表图像的像素值(从0到65535)。

给定表示填充的开始像素(行和列)的坐标(sr,sc)和像素值newColor,“填充”图像。

为了执行“填充填充”,考虑起始像素,加上与起始像素相同颜色的起始像素,以及与这些像素4方向连接的任何像素的4方向连接的任何像素(也与颜色相同起始像素)等等。用newColor替换上述所有像素的颜色。

最后,返回修改后的图像。

解题方法

方法一:DFS

经典的dfs方法,要保存一下指定位置的颜色再对该位置染色,并对其四个方向的相邻元素进行处理,如果颜色和以前的颜色相同即染色并递归调用。

class Solution(object):
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
SR, SC = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r - 1, c)
if r < SR - 1: dfs(r + 1, c)
if c >= 1: dfs(r, c - 1)
if c < SC - 1: dfs(r, c + 1)
dfs(sr, sc)
return image

方法二:BFS

使用BFS,需要注意的是如果起始位置的颜色等于新的颜色,那么直接返回掉,否则后面会死循环。

class Solution:
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
que = collections.deque()
que.append((sr, sc))
start = image[sr][sc]
if start == newColor: return image
M, N = len(image), len(image[0])
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while que:
pos = que.popleft()
image[pos[0]][pos[1]] = newColor
for d in directions:
newx, newy = pos[0] + d[0], pos[1] + d[1]
if 0 <= newx < M and 0 <= newy < N and image[newx][newy] == start:
que.append((newx, newy))
return image

日期

2018 年 2 月 28 日
2018 年 11 月 14 日 —— 很严重的雾霾

【LeetCode】733. Flood Fill 解题报告(Python)的更多相关文章

  1. LN : leetcode 733 Flood Fill

    lc 733 Flood Fill 733 Flood Fill An image is represented by a 2-D array of integers, each integer re ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【Leetcode_easy】733. Flood Fill

    problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. Golang使用validator进行数据校验及自定义翻译器

    Golang使用validator进行数据校验及自定义翻译器 包下载:go get github.com/go-playground/validator/v10 一.概述 在接口开发经常会遇到一个问题 ...

  2. Python基础之流程控制if判断

    目录 1. 语法 1.1 if语句 1.2 if...else 1.3 if...elif...else 2. if的嵌套 3. if...else语句的练习 1. 语法 1.1 if语句 最简单的i ...

  3. Redis学习小结

    在7月中旬,我成功入职实习,通过进入公司,认识到了个人与企业巨大的差距,首先就是对于中间件的使用,ElasticSearch.Redis.Kafka等等,都是听过却从未使用过的,然而在任务下达之后,激 ...

  4. 【学相伴】Nginx最新教程通俗易懂-狂神说

    Nginx - 学相伴 分享人:秦疆(遇见狂神说) 公司产品出现瓶颈? 我们公司项目刚刚上线的时候,并发量小,用户使用的少,所以在低并发的情况下,一个jar包启动应用就够了,然后内部tomcat返回内 ...

  5. Yarn 公平调度器案例

    目录 公平调度器案例 需求 配置多队列的公平调度器 1 修改yarn-site.xml文件,加入以下从参数 2 配置fair-scheduler.xml 3 分发配置文件重启yarn 4 测试提交任务 ...

  6. HDFS04 HDFS的读写流程

    HDFS的读写流程(面试重点) 目录 HDFS的读写流程(面试重点) HDFS写数据流程 网络拓扑-节点距离计算 机架感知(副本存储节点的选择) HDFS的读数据流程 HDFS写数据流程 客服端把D: ...

  7. 学习java 7.9

    学习内容: Date类 Date类常用方法 SimpleDateFormat 1.格式化(从Date到String) public final String format(Date date) 将日期 ...

  8. Hbase(一)【入门安装及高可用】

    目录 一.Zookeeper正常部署 二.Hadoop正常部署 三.Hbase部署 1.下载 2.解压 3.相关配置 4.分发文件 5.启动.关闭 6.验证 四.HMaster的高可用 一.Zooke ...

  9. 转 onSaveInstanceState()和onRestoreInstanceState()使用详解

    转 https://www.jianshu.com/p/27181e2e32d2 背景 如果系统由于系统约束(而不是正常的应用程序行为)而破坏了Activity,那么尽管实际 Activity实例已经 ...

  10. vue-cli4脚手架搭建一

    涉及内容 html  css   javascript   node.js   npm    webpack 2.9.6是常用版本 vue-cli4是基于webpack的 webpack是基于node ...