LEETCODE —— Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
For 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 反向思考,可以从四边开始找O,这些外面的O都找出来以后剩下的就是被包围的O了,非递归的BFS和DFS都可以AC(递归的很可能爆栈),找出外面的O并替换为A,剩下的所有的O就是要找的,flip为X即可,再把A替换为O
AC之后的结果来看,DFS所需时间是BFS两倍。 BFS实现:
class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
if board==[]:
return
width = len(board[0])
height = len(board)
edgeNods=[]
for column in range(width): #todo: all edgeNode
edgeNode = (0, column)
if board[0][column]=='O':
edgeNods.append(edgeNode)
for line in range(height): #todo: all edgeNode
edgeNode = (line, 0)
if board[line][0]=='O':
edgeNods.append(edgeNode)
for column in range(width): #todo: all edgeNode
edgeNode = (height-1, column)
if board[height-1][column]=='O':
edgeNods.append(edgeNode)
for line in range(height): #todo: all edgeNode
edgeNode = (line, width-1)
if board[line][width-1]=='O':
edgeNods.append(edgeNode) stack = edgeNods
while stack:
nod = stack.pop(0)
if board[nod[0]][nod[1]] == 'X' or board[nod[0]][nod[1]] == 'A' :
continue
else:
board[nod[0]][nod[1]] = 'A' neighbours=[]
neighbours.append((nod[0]-1, nod[1]))
neighbours.append((nod[0]+1, nod[1]))
neighbours.append((nod[0], nod[1]-1))
neighbours.append((nod[0], nod[1]+1))
for neiber in neighbours:
if neiber[0]<0 or neiber[0] >= height \
or neiber[1] <0 or neiber[1] >= width:
continue
if board[neiber[0]][neiber[1]] == 'X' or board[neiber[0]][neiber[1]] == 'A' :
continue
if (neiber in stack):
continue
stack.append(neiber) for column in range(width):
for line in range(height):
if board[line][column] == 'O':
board[line][column]= "X"
for column in range(width):
for line in range(height):
if board[line][column] == 'A':
board[line][column]='O'
LEETCODE —— Surrounded Regions的更多相关文章
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- LeetCode: Surrounded Regions 解题报告
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- Leetcode: Surrounded regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- [LeetCode] Surrounded Regions 广度搜索
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [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 (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- Python学习基本
刚开始学习是看了这个网站: 廖雪峰的官方网站 http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac92707 ...
- 设置Windows 7 防火墙端口规则
http://jingyan.baidu.com/article/c843ea0b7d5c7177931e4ab1.html?qq-pf-to=pcqq.c2c 主要解决手机访问pc站点的问题(pc和 ...
- 为什么eclipse中代码提示错误,但是项目目录却不提示错误
public class Aasf { public static void main(String[] args) { System.out.println("");aihfsa ...
- Android性能优化之使用线程池处理异步任务
转:http://blog.csdn.net/u010687392/article/details/49850803
- Activity 生命周期
Activity 的四种基本状态 1.运行态(Running) Activity 处于屏幕最前端,用户可见且获得焦点. 2.暂停态(Paused) Activity被置于后台,用户可见,但失去焦点 3 ...
- 《嫌疑人X的献身》书评
<嫌疑人X的献身>是日本著名推理小说作家东野圭吾的代表作之一.1985年东野圭吾以一本<放学后>出道,出道初期善于写精巧细致的本格推理,后期文笔愈发老辣.简练.2005年东野圭 ...
- height与line-height
1.网页的所有元素可以分为块元素和行元素.一行文字所在的一个逻辑区域是行元素,其他的元素就都是块元素line-height只针对行元素,height针对其他所有元素 2. width,height对于 ...
- Shell if 判断之detail
参考: http://blog.chinaunix.net/uid-20788517-id-3135826.html
- esxi 6 添加硬盘、网卡
添加硬盘 esxi系统装完之后,直接再接上一块硬盘,然后再使用管理工具添加硬盘 打开VMware vSphere Client,登录esxi服务器, 打开配置-存储器-选择添加存储器 选择磁盘 这里能 ...
- pyqt5
使用python爬虫需要使用webkit执行javascript,查找资料可使用pyqt5,在此记录下安装步骤 windows 安装pyqt5 操作webkit 可用版本 1. 下载python 3. ...