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 ...
随机推荐
- Httpclient请求数据
package com.baidu.myutils; import java.io.IOException; import org.apache.http.HttpEntity; import org ...
- EF实体框架数据操作基类(转)
//----------------------------------------------------------------// Copyright (C) 2013 河南禄恒软件科技有限公司 ...
- mysql 查询优化
不说话,先贴代码 public PageResult<BoTmcRaw> getLargeList(BaseCondition baseCondition) { PageResult< ...
- Delphi编译的程序如何获取管理员权限
1.制作manifest文件 <?xml version="1.0" encoding="UTF-8" standalone="yes" ...
- ie7 用 clearfix 清除浮动时遇到的问题
<!doctype html> <html> <head> <style> .fr{float:right;display:inline} li{bor ...
- 有关javascript的性能优化(合理的管理内存)
使用具备垃圾收集机制的语言编写程序,开发人员一般不必操心内存管理的问题.但是,Javascript在进行内存管理及收集时面临的问题是有点与众不同.其中最主要的一个问题是分配给Web浏览器的可用内存数量 ...
- 拓扑排序&&欧拉(回)路
摘要:最近是不适合写代码么?忘记初始化wa到死<_=_=_>.唔--最近在学习图论,从基础搞起,先搞了拓扑排序和欧拉(回)路. Part 0. 拓扑排序 ==挖坑== Part 1. 欧拉 ...
- Three.js基本 Demo
对于新手来说,几个简单的例子非常实用,偶然发现几个不错的Demo,分享给大家! Three.js基本 Demo 1.最基本的Hello World:http://stemkoski.github.io ...
- C# DataGridView使用记录分享
最近使用DataGridView,把其中遇到的问题和一些知识记录下来,以便以后用到的时候可以快速的想起. 1.添加行号 有时我们在使用DataGridView时会被要求添加在每一行数据前面添加上行号, ...
- 理解python的with语句
Python’s with statement provides a very convenient way of dealing with the situation where you have ...