[LeetCode] Flood Fill 洪水填充
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:
- The length of
image
andimage[0]
will be in the range[1, 50]
. - The given starting pixel will satisfy
0 <= sr < image.length
and0 <= sc < image[0].length
. - The value of each color in
image[i][j]
andnewColor
will be an integer in[0, 65535]
.
这道题给了一个用二维数组表示的图像,不同的数字代表不同的颜色,给了一个起始点坐标,还有一个新的颜色,让我们把起始点的颜色以及其相邻的同样的颜色都换成新的颜色。实际上就是一个找相同区间的题,可以用 BFS 或者 DFS 来做。先来看 BFS 的解法,使用一个队列 queue 来辅助,首先将给定点放入队列中,然后进行 while 循环,条件是 queue 不为空,然后进行类似层序遍历的方法,取出队首元素,将其赋值为新的颜色,然后遍历周围四个点,如果不越界,且周围的颜色跟起始颜色相同的话,将位置加入队列中,参见代码如下:
解法一:
- class Solution {
- public:
- vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
- int m = image.size(), n = image[].size(), color = image[sr][sc];
- vector<vector<int>> res = image;
- vector<vector<int>> dirs{{,-},{-,},{,},{,}};
- queue<pair<int, int>> q{{{sr, sc}}};
- while (!q.empty()) {
- int len = q.size();
- for (int i = ; i < len; ++i) {
- auto t = q.front(); q.pop();
- res[t.first][t.second] = newColor;
- for (auto dir : dirs) {
- int x = t.first + dir[], y = t.second + dir[];
- if (x < || x >= m || y < || y >= n || res[x][y] != color) continue;
- q.push({x, y});
- }
- }
- }
- return res;
- }
- };
DFS 的写法相对简洁一些,首先判断如果给定位置的颜色跟新的颜色相同的话,直接返回,否则就对给定位置调用递归函数。在递归函数中,如果越界或者当前颜色跟起始颜色不同,直接返回。否则就给当前位置赋上新的颜色,然后对周围四个点继续调用递归函数,参见代码如下:
解法二:
- class Solution {
- public:
- vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
- if (image[sr][sc] == newColor) return image;
- helper(image, sr, sc, image[sr][sc], newColor);
- return image;
- }
- void helper(vector<vector<int>>& image, int i, int j, int color, int newColor) {
- int m = image.size(), n = image[].size();
- if (i < || i >= m || j < || j >= n || image[i][j] != color) return;
- image[i][j] = newColor;
- helper(image, i + , j, color, newColor);
- helper(image, i, j + , color, newColor);
- helper(image, i - , j, color, newColor);
- helper(image, i, j - , color, newColor);
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/733
类似题目:
参考资料:
https://leetcode.com/problems/flood-fill/
https://leetcode.com/problems/flood-fill/discuss/109584/java-9-liner-dfs
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Flood Fill 洪水填充的更多相关文章
- LeetCode刷题 Flood Fill 洪水填充问题
An image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...
- LeetCode - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 【LeetCode】733. Flood Fill 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill)
Leetcode之深度优先搜索(DFS)专题-733. 图像渲染(Flood Fill) 深度优先搜索的解题详细介绍,点击 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 ...
- [LeetCode&Python] Problem 733. Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- 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 ...
- [Swift]LeetCode733. 图像渲染 | Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
随机推荐
- 将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限等等. 一.用户权限模型 为实现一个较为灵活的用户权限数据模 ...
- linux小白成长之路6————安装Java+Apache(httpd)+Tomcat
[内容指引] 安装Java环境: 查看JDK版本: 安装Apache(httpd); 安装Tomcat: 设置服务开机启动. 1.安装Java环境 指令: yum intall java-1.8.0* ...
- python全栈学习--day8
一,文件操作基本流程. 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众 ...
- Redux----Regular的Redux实现整理
Regular的Redux实现整理 什么问题? 组件的树形结构决定了数据的流向,导致的数据传递黑洞 怎么解决? 所有组件都通过中介者传递共享数据 方案: 中介者: (function create ...
- MySQL数据库操作类(PHP实现,支持连贯操作)
<?php /** * Author: suvan * CreateTime: 2018/2/27 * description: 数据库操作类(仅对接MySQL数据库,主要利用MySQLi函数) ...
- 冲刺NO.10
Alpha冲刺第十天 站立式会议 项目进展 项目核心功能逐步构建完成,测试工作也已开始.主要对部分功能组合进行测试以测试系统可用性. 问题困难 项目的主要困难在这个时间点主要存在于测试工作中,测试工作 ...
- Java暑期作业
一.假期观影笔记--<熔炉> 影片<熔炉>是根据发生在韩国光州聋哑学校里的真实事件而改编.影片讲述的是在一所聋哑儿童学校里,校长.教务以及老师披着慈善的华丽外衣对学校中的多名未 ...
- 学号:201621123032 《Java程序设计》第5周学习总结
1:本周学习总结 1.1: 写出你认为本周学习中比较重要的知识点关键词 接口interface,comparator接口和comparable接口. 1.2:尝试使用思维导图将这些关键词组织起来. 2 ...
- Android实验报告
实验名称:Android程序设计 实验时间:2017.5.24 实验人员:20162309邢天岳(结对同学20162313苑洪铭) 实验目的:使用android stuidio开发工具进行基本安卓软件 ...
- iOS Storyboard unwind segues使用小结
使用storyboard开发的时候,经常会在一个scene上添加一个button,再拖拽这个button到某个想要关联的页面,最后选择push的方式跳转.这样scene_A和scene_B就有了一个& ...