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. __attribute__((packed))作用

    1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关,g ...

  2. eap-md5

    eap-md5       文件路径 用途 示例 备注 #gedit /usr/local/etc/raddb/sites-available/default #gedit /usr/local/et ...

  3. 2.4G/5G频段WLAN各国使用信道表

    List of WLAN channels (维基百科):https://en.wikipedia.org/wiki/List_of_WLAN_channels 2.4G 5G 另附美国5G允许使用的 ...

  4. python中if __name__ == '__main__': 的解析

    当你打开一个.py文件时,经常会在代码的最下面看到if __name__ ==  '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一 ...

  5. export a java project to runable jar

    When a java project needs to be transfered to another machine, e.g. vps, we need to export it to a r ...

  6. [蟒蛇菜谱]Python函数参数传递最佳实践

    将函数作为参数传递,同时将该函数需要的参数一起传递.可参考threading.Timer的处理方式: class threading.Timer(interval, function, args=[] ...

  7. C语言程序设计第七次作业

    一.学习内容     本次课学习了函数的基本知识,需要大家对如下知识点进行总结:     1. 函数定义的基本格式,函数定义和函数原型(声明)的区别何在?     2. 函数的调用方式有哪几种     ...

  8. 北大poj-1011

    木棒 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 136132   Accepted: 32036 Description ...

  9. Hello World!

    博客园的效率真是高啊,开通博客的申请刚刚递交几分钟就通过了,赞一下博客园的程序员们,这么晚还在工作! 博客主要用来记录自己学习HTML5.CSS.PHP等web前端技术的经历,因为是初学者,所以发的文 ...

  10. mov和ldr/str的区别

    ARM是RISC结构,数据从内存到CPU之间的移动只能通过L/S指令来完成,也就是ldr/str指令.比如想把数据从内存中某处读取到寄存器中,只能使用ldr比如:ldr r0, 0x12345678就 ...