[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 ...
随机推荐
- 智能文件选择列表—— bat 批处理
智能文件选择列表 *.wim @echo off setlocal enabledelayedexpansion title 智能文件选择列表 pushd %~dp0 & cd /d %~dp ...
- Python不支持函数重载
函数重载与Python: 函数重载的好处就是不用为了不同的参数类型或参数个数,而写多个函数.多个函数用同一个名字,但参数表,即参数的个数和数据类型可以不同.调用的时候,虽然方法名字相同,但根据参数表可 ...
- MSI/MSI-X Capability结构 (转)
http://blog.sina.com.cn/s/blog_6472c4cc0102dskj.html
- linux 下安装 redis
https://redis.io/ 1.下载 $ cd /usr/local/ $ wget http://download.redis.io/releases/redis-4.0.7.tar.gz ...
- autofac生命周期入门(如何避免内存泄漏)
如果你是一个IOC新手,那么生命周期可能会比较难以理解.以至于谈到这个问题时,一些老手也时常表示疑虑和害怕.一个令人不安的问题就是-对象没有在合适的时机被销毁.这样一来内存的使用率就会一直攀升,直到程 ...
- HDU 6229 - Wandering Robots - [概率题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6229 转载: https://blog.csdn.net/Anna__1997/article/det ...
- 信1705-2 软工作业最大重复词查询思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词)数组中。 (2)定义一个Map,key是字符串类型,保存单词;value是数字类型,保存该单词出现的次数。 (3)遍历(1)中得到的字符串数组,对于每一个单词,考察Map的key中是否出现过该单词,如果没出现过,map中增加一个元素,key为该单词,value为1(
通过学习学会了文本的访问,了解一点哈希表用途.经过网上查找做成了下面查询文章重复词的JAVA程序. 1 思 思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词 ...
- cordova 内部API 用ssl https,报错
环境:node6.10.1 cordova 6.x, ionic 2.2.1 用cordova/ionic 建立的app我们的api 地址要用https,做了安全加密之后,按照正常的流程,打包,然后跑 ...
- Multiple SSH keys for different accounts on Github or Gitlab
[inside this square brackets give a name to the followed acc.] name = github_username email = github ...
- [daily][centos] redhat增加扩展仓库
https://rpmfusion.org/Configuration sudo yum localinstall --nogpgcheck https://download1.rpmfusion.o ...