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 and image[0] will be in the range [1, 50].
  • The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
  • The value of each color in image[i][j] and newColor will 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的更多相关文章

  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] 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 ...

  3. [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 ...

  4. [LeetCode] 529. Minesweeper_ Medium_ tag: BFS

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  5. [LeetCode] 690. Employee Importance_Easy tag: BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 原生js--表单

    阅读了<javascript权威指南>P396-P409. 一.表单和表单元素的选取 1.选取表单(包含name=“address”属性的form表单) document.querySel ...

  2. ELK系列七:Elasticsearch的集群配置和监控以及在部署ELK中踩的坑

    1.基本下载安装 #按照ELK系列一博客安装启动即可,没有大坑,注意一下权限即可 chmod -R 777 ./elasticsearch #此外没有java的,注意安装下JDK,我这次部署的环境是C ...

  3. 编译安装的gitlab8.x如何修改时区设置

    编译安装的gitlab 8.x版本默认的时区是UTC,在页面上显示的时间默认是零时区的区时,安装完成之后,如果页面上显示的时间比北京时间少了8个小时,则需要修改一下时区 把gitlab.yml文件中的 ...

  4. 单例模式全面学习(C++版)

    单例模式:用来创建独一无二的,只能够有一个实例的对象. 单例模式的结构是设计模式中最简单的,但是想要完全实现一个线程安全的单例模式还是有很多陷阱的,所以面试的时候属于一个常见的考点~ 单例模式的应用场 ...

  5. nginx 反向代理apache服务器 配置java与PHP共存环境

    listen 80; listen 443; ssl on; ssl_certificate /passport.crt; ssl_certificate_key /passport.key; ssl ...

  6. iptables、防火墙配置、NAT端口映射

    一,配置一个filter表放火墙 (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n Chain INPUT (policy ACCEPT) targ ...

  7. centos6.5安装logwatch监控日志

    Logwatch是使用 Perl 开发的一个日志分析工具Logwatch能够对Linux 的日志文件进行分析,并自动发送mail给相关处理人员,可定制需求Logwatch的mail功能是借助宿主系统自 ...

  8. HDFS体系结构(NameNode、DataNode详解)

    hadoop项目地址:http://hadoop.apache.org/ NameNode.DataNode详解 (一)分布式文件系统概述 数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配 ...

  9. pycurl实例详解

    Pycurl是Python的libcurl接口.liburl是客户端的URL传输库,它支持FTP,FTPS,HTTP,HTTPS,TELNET,LDAP等诸多协议,同时支持HTTP认证,代理,FTP上 ...

  10. Gym-101375C MaratonIME eats japanese food 初始化struct技巧

    题意:两种操作:A ,R 分别有三个参数,表示放或者移走(x,y)处半径为r的盘子.5000次操作,每次操作判断是否可行. 题解:两种情况不行:1.相交2.放到桌子外面.5000的数据直接模拟,删除操 ...