[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 region is captured by flipping all 'O's into 'X's in that surrounded region.
Example:
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
Explanation:
Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
这个题目思路实际上跟[LeetCode] 733. Flood Fill_Easy tag: BFS很像, 只不过我们将array遍历两边,第一次遍历边框, 如果是'O' 将所有相邻的'O' 都标记为visited, 然后第二次遍历
如果没有标记为visited, 将其换为'X'即可.
1. Constriants
1) None or n == 0
2) element will be only 'X' or 'O'
2. Ideas
DFS/BFS T: O(m*n) S; O(m*n)
3. Code
3.1) DFS
class Solution:
def surroundRegion(self, board):
if not board or len(board[0]) == 0: return
lr, lc , visited = len(board), len(board[0]), set()
def dfs(r, c):
if 0 <= r < len(board) and 0 <= c < len(board[0]):
if (r,c) not in visited and board[r][c] == 'O':
visited.add((r,c))
dfs(r+1, c)
dfs(r-1, c)
dfs(r,c+1)
dfs(r,c-1) for i in range(lr):
for j in range(lc):
if (i== 0 or i == lr-1 or j == 0 or j == lc -1 ) and board[i][j] == 'O' and (i,j) not in visted:
dfs(i,j)
for i in range(lr):
for j in range(lc):
if board[i][j] == 'O' and (i,j) not in visited:
board[i][j] = 'X'
3.2) BFS
class Solution:
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
if not board or len(board[0]) == 0 : return
lr, lc, queue, visited = len(board), len(board[0]), collections.deque(), set()
for i in range(lr):
for j in range(lc):
if (i == 0 or i == lr - 1 or j == 0 or j == lc -1) and board[i][j] == 'O' :
queue.append((i,j))
visited.add((i,j))
dirs = [(0,1), (0,-1), (1,0), (-1,0)]
while queue:
pr, pc = queue.popleft()
for c1, c2 in dirs:
nr, nc = pr + c1 , pc + c2
if 0 <= nr < lr and 0 <= nc <lc and (nr, nc) not in visited and board[nr][nc] == 'O':
queue.append((nr, nc))
visited.add((nr, nc)) for i in range(lr):
for j in range(lc):
if board[i][j] == 'O' and (i,j) not in visited: # first check (i, j) not in visited will speed up the process a lot.
board[i][j] = 'X'
[LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS的更多相关文章
- leetcode 130 Surrounded Regions(BFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- Leetcode 130 Surrounded Regions DFS
将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- leetcode 130. Surrounded Regions----- java
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 130. Surrounded Regions (Graph; DFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
随机推荐
- M - Sum
XXX is puzzled with the question below: 1, 2, 3, ..., n (1<=n<=400000) are placed in a line. T ...
- MySql 远程连接的条件
1.首先看服务器防火墙 引用:http://www.cnblogs.com/silent2012/archive/2015/07/28/4682770.html CentOS 7.0默认使用的是fir ...
- imu内参标定
https://medium.com/@tomas789/iphone-calibration-camera-imu-and-kalibr-33b8645fb0aa how kalibr model ...
- Robot Framework使用For循环
1.普通的For循环 在一个普通的For循环中,循环开始的关键字是 :FOR ,其中的:用于与一般关键字做区分,对于循环结构体内的每一行,使用 \ 作为改行的行首关键字.对于循环中的变量,可以在 IN ...
- [No0000CE]检测非空格字符作为密码的密码强度
Regex.Replace(pwd, "^(?:([a-z])|([A-Z])|([0-9])|(.)){6,}|(.)+$", "$1$2$3$4$5").L ...
- Java之旅_高级教程_多线程编程
摘自:http://www.runoob.com/java/java-multithreading.html Java 多线程编程 Java 给多线程编程提供了内置的支持.一条线程指的是进程中的一条执 ...
- ipv6的校验格式
ipv6的校验格式: ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
- java ftp上传文件
/** * 上传文件到ftp * @param server * @param user * @param pwd * @param filenames */ public static void u ...
- mysql之索引简介
索引分类 mysql在存储数据时,是按着主键的顺序存储的.主键索引是物理索引,其他索引都是逻辑索引. 普通索引 普通索引是最基本的索引,没有任何限制的索引,普通索引列的数据可以重复.其唯一的任务就是加 ...
- Maven项目Update Project后JRE System Library自动变回1.5解决办法
最近在搭建Spring Boot项目<一步步搭建 Spring Boot maven 框架的工程>的时候,虽然设置JRE System Library为1.8,但是,当我 用 Maven ...