[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 ...
随机推荐
- A - Cable master
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...
- Saltstack设置安装源为阿里源
Saltstack设置安装源为官方源有时候在国内网络不好安装较慢或者安装不上,可设置为阿里源 比如对于 Centos 7 系统,在 saltstack 的官网提供的配置初始化手册是: sudo yum ...
- 洛谷P1219 八皇后【dfs】
题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...
- hdu4763 Theme Section【next数组应用】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- ASP.NET MVC 系统过滤器、自定义过滤器
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- 用NFS挂载root出现:NFS: failed to create MNT RPC client, status=-101(-110)
2014-02-18 08:06:17 By Ly #Linux 阅读(78) 评论(0) 错误信息如下: Root-NFS: nfsroot=/home/zenki/nfs/rootfs NFS ...
- 编译openssl和Apache报错checking for SSL_CTX_new... no
执行export LDFLAGS=-ldl命令后重新编译
- php之memcached存储session配置、存储、获取
[session] ①.session.save_handler = memcache session.save_handler 定义了来存储和获取与会话关联的数据的处理器的名字,默认是files ② ...
- Android Studio自动排版的两种方法
Android Studio这样的集成开发环境虽然代码自动化程度很高,但是自动化程度高导致人的自主性就下降了,而且总是依赖编辑器的功能也会搞得代码排版很别扭. 最难受的是你在Android Studi ...
- vulnerability test
vegas ---go--https://zhuanlan.zhihu.com/p/21826478 locust---python jmeter--java---http://www.cnblogs ...