[LeetCode] 733. Flood Fill_Easy tag: BFS
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
imageandimage[0]will be in the range[1, 50]. - The given starting pixel will satisfy
0 <= sr < image.lengthand0 <= sc < image[0].length. - The value of each color in
image[i][j]andnewColorwill be an integer in[0, 65535].
因为是2D-array的题目, 然后上下左右4个方向, 跟相对位置有关, 那么实际上跟distinct island很像, 那么很基础的做法, BFS. 此时要注意的是, 如果用BFS, 那么很有可能你的邻居后面可能再次找你, 所以记住, 要用BFS的时候, 判断需不需要加入visited 这样一个set来避免重复的判断某一个元素, 甚至无法跳出BFS的循环.
所以按照hiredintech上面的canvas的步骤, 一步一步来,
1. constraints:
1) image 的size给定了, 至少不用判断image的行或者列可能为空的情况.
2) 每个pixel的值给定了i,并且newcolor的值也给定 [0, 65535], 所以不用判断newcolor的范围.
3) image的max size给定了, 最大[50]*[50]
4) sr跟sc 默认为有效值, 很好, 不用判断了.
2. Ideas:
BFS: assum m*n array
T: O(m*n) 因为要扫到array的每一个元素
S: O(m*n) 因为queue可能最多把整个array都append进去.
1) edge case, newcolor == oricolor, 直接返回原array
2) queue, 初始化将(sr,sc) 放入
3) 如果queue非空, popleft, 然后ans[nr][nc] = newcolor, visited.add(nr, nc)
4) 然后判断4个directions, 另外注意还要判断是否已经visited过了.
3) 如果邻居有效, 并且color == oricolor, 没有visited过, queue.append(邻居)
4) 返回 ans
3. code:
class Solution:
def floodfill(self, image, sr, sc, newColor):
lr, lc, oricolor, ans = len(image), len(image[0]), image[sr][sc], image
if oricolor == newColor: return ans
queue, visited, dirs = collections.deque([(sr, sc)]), set(), [(0,1), (0, -1), (1, 0), (-1, 0)]
while queue:
pr, pc = queue.popleft()
ans[pr][pc] = newColor
visited.add((pr, pc))
for d1, d2 in dirs:
nr, nc = pr + d1, pc + d2
if 0 <= nr < lr and 0 <= nc < lc and image[nr][nc] == oricolor and (nr, nc) not in visited:
queue.append((nr, nc))
return ans
4. test cases:
1. newcolor == oricolor
2.
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]]
[LeetCode] 733. Flood Fill_Easy tag: BFS的更多相关文章
- 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 ...
- [LeetCode] 490. The Maze_Medium tag: BFS/DFS
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- [LeetCode] 529. Minesweeper_ Medium_ tag: BFS
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- [LeetCode] 690. Employee Importance_Easy tag: BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
Given a string S and a character C, return an array of integers representing the shortest distance f ...
随机推荐
- 【Drools-开源业务规则引擎】入门实例(含源码)
该实例转自:http://blog.csdn.net/quzishen/article/details/6163012 便于理解的应用实例1: 现在我们模拟一个应用场景:网站伴随业务产生而进行的积分发 ...
- 【转】strlen源码
strlen源码剖析 学习高效编程的有效途径之一就是阅读高手写的源代码,CRT(C/C++ Runtime Library)作为底层的函数库,实现必然高效.恰好手中就有glibc和VC的CRT源代码, ...
- JS-缓冲运动基础结构
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 移动端rem自适应布局(切图)
本篇适用于初次使用rem为单位切图而无从下手的童鞋.核心是根据屏幕动态改变根元素字体大小,以达到适配各种屏幕.这只是一个拿来就用的教程.很多东西没有详细说明.不过对于快速做手机端切图很有帮助. 模板: ...
- SharpGL学习笔记(八) 矩阵堆栈和变换的综合例子: 机器人
我们先引入关于"矩阵堆栈"的官方说法: OpenGL的矩阵堆栈指的就是内存中专门用来存放矩阵数据的某块特殊区域.实际上,在创建.装入.相乘模型变换和投影变换矩阵时,都已用到堆栈操作 ...
- python nose测试框架中使用allure_report框架
在使用nose自带的xunit生成xml文件生成测试报告后,领导说报告不够炫,没有百分比效果,且在web自动化时的截图不美观,html很多情况下没有显示图片(nose框架截图方法这里),正好,allu ...
- Jmeter TCP取样器配置及发送图解
最近在通过Jmeter测试TCP发送请求时,遇到相关问题,现记录 查看管方文档,TCP发送有三种启用方式: TCPClientImpl:文本数据,默认为这种 BinaryTCPClientImpl:传 ...
- 常见的几个js疑难点,match,charAt,charCodeAt,map,search
JavaScript match() 方法 定义和用法 match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastI ...
- R因子
factor(x = character(), levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA) l ...
- win10中强制vs2015使用管理员启动
文章转自: win10中强制vs2015使用管理员启动 首先,和网上流传的版本一样,需要做这下面这两步: 1. 打开VS快捷方式的属性对话框. 2.勾选“用管理员身份运行” 现在,你双击V ...