Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium

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的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  3. LeetCode: Surrounded Regions 解题报告

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  4. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  5. Leetcode: Surrounded regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  6. LeetCode: Surrounded Regions [130]

    [题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...

  7. [LeetCode] Surrounded Regions 广度搜索

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  8. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  9. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. NethServer 7.2 RC1,增加深度数据包检测

    NethServer 7.2 RC1 "Saltimbocca"  发布了,NethServer是基于CentOS的面向服务器的Linux发行.该产品的主要特性是模块化的设计,这使 ...

  2. Python开发入门与实战14-基于Extjs的界面

    14. 基于Extjs的界面 上一章我们实现了一个原生的html例子,本章我们将采用Extjs实现界面的展现,来说明MVC模式下我们是怎么考虑界面与业务层的关系的. 14.1. 引用Extjs目录 首 ...

  3. MVC 实体字段自定义验证

    [Remote("ActionName", "ControllerName", AdditionalFields = "ID", Error ...

  4. ADO.NET与ORM的比较:NHibernate实现CRUD(转)

    原文地址 http://blog.csdn.net/zhoufoxcn/article/details/5402511 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spr ...

  5. sqlite的常用语法

    sqllite 增删改查创建表的语法 创建表db.execSQL("create table user(_id integer primary key autoincrement,numbe ...

  6. 关于JavaEE 开发中web.xml的主要配置及其使用

    web.xml 中的listener. filter.servlet 加载顺序及其详解 在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人 ...

  7. java nio(non-blocking io)简介及和io

    在 Java1.4之前的I/O系统中,提供的都是面向流的I/O系统,系统一次一个字节地处理数据,一个输入流产生一个字节的数据,一个输出流消费一个字节 的数据,面向流的I/O速度非常慢,而在Java 1 ...

  8. js zTree的用法

    代码如下: <script type="text/javascript">    var reginTree = {    setting: {        view ...

  9. 长沙市轨道交通工程BIM应用招标公告

    摘要: 长沙市轨道交通集团有限公司对其长沙市轨道交通3号线一期工程建筑信息模型(BIM)技术应用项目进行国内公开招标 长沙市轨道交通集团有限公司对其长沙市轨道交通3号线一期工程建筑信息模型(BIM)技 ...

  10. 阿里云弹性Web托管的URL重写问题

    今天将ThinkPHP写的网站搭到阿里云的弹性Web托管服务器上,出现路由问题 诸如访问 www.xxx.com/home/index.html会发生错误如下 页面报错: No input file ...