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.

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 keys: 1. BFS from the most outer layer.
 class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
""" if not board:
return m = len(board)
n = len(board[0]) queue = [] for i in range(m):
for j in range(n):
if i == 0 or j == 0 or i == m -1 or j == n-1:
if board[i][j] == 'O':
queue.append([i,j]) while queue:
[p,q] = queue.pop(0)
board[p][q] ='V'
if self.isValid(p+1, q, m, n) and board[p+1][q] == 'O':
queue.append([p+1, q])
if self.isValid(p-1, q, m, n) and board[p-1][q] == 'O':
queue.append([p-1, q])
if self.isValid(p, q+1, m, n) and board[p][q+1] == 'O':
queue.append([p, q+1])
if self.isValid(p, q-1, m, n) and board[p][q-1] == 'O':
queue.append([p, q-1]) for i in range(m):
for j in range(n):
if board[i][j] == 'V':
board[i][j] ='O'
elif board[i][j] == 'O':
board[i][j] ='X' def isValid(self, i,j, m, n):
return i>0 and i<m and j>0 and j<n
												

Leetcode 130. Surrounded Regions的更多相关文章

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

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

  2. Java for LeetCode 130 Surrounded Regions

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

  3. leetcode 130 Surrounded Regions(BFS)

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

  4. 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 ...

  5. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  6. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

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

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

  8. [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 reg ...

  9. 【leetcode】Surrounded Regions

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

随机推荐

  1. 关于IOS免证书真机安装的过程和问题

    由于本人是边工作边转的IOS,所以一直都没怎么使用过免证书安装过程,通常都是公司申请的99美元的账号直接开发.但是前两天有个朋友需要在展会上用的Ipad上安装内网应用,申请一个苹果账号还要审核前后加起 ...

  2. iOS:CYLTabBarController【低耦合集成TabBarController】

    导航 与其他自定义TabBarController的区别 集成后的效果 项目结构 使用CYLTabBarController 第一步:使用CocoaPods导入CYLTabBarController ...

  3. Resting state brain networks derived from spatial ICA - an individual case

    Reference: Xin D, Biswal B B. Dynamic brain functional connectivity modulated by resting-state netwo ...

  4. 嵌入支付宝SDK,出现“LaunchServices: ERROR: There is no registered handler for URL scheme alipay”错误

    应用项目中嵌入支付宝SDK,在模拟器运行app后,会出现“LaunchServices: ERROR: There is no registered handler for URL scheme al ...

  5. TinyFrame升级之一:框架概览

    由于之前的TinyFrame多于简单,并且只是说明原理,并无成型的框架出来,所以这次我把之前的知识进行了汇总,然后做出了这一版的TinyFrame框架. 整个框架的结构如下: TinyFrame.Da ...

  6. MFC 调试方法

    AfxDebugBreak     MFC 提供特殊的 AfxDebugBreak 函数,以供在源代码中对断点进行硬编码:     AfxDebugBreak( ); 在 Intel 平台上,AfxD ...

  7. 数据字典生成工具之旅(3):PowerDesign文件组成结构介绍及操作

    从这篇开始将正式讲解整个重要部分的实现细节,本篇讲解Pdm文件的解析.其实PDM文件就是XML文件,可以用Editplus或者VS打开查看.了解到这一点之后大家就能猜到,可以用解析XML的方式读取PD ...

  8. 各种主流 SQLServer 迁移到 MySQL 工具对比

          我之所以会写这篇对比文章,是因为公司新产品研发真实经历过这个痛苦过程(传统基于SQL Server开发的C/S产品转为MySQL云产品).首次需要数据转换是测试环节,当时为了快速验证新研发 ...

  9. 误人子弟的网络,谈谈HTTP协议中的短轮询、长轮询、长连接和短连接

    引言 最近刚到公司不到一个月,正处于熟悉项目和源码的阶段,因此最近经常会看一些源码.在研究一个项目的时候,源码里面用到了HTTP的长轮询.由于之前没太接触过,因此LZ便趁着这个机会,好好了解了一下HT ...

  10. gocode+auto-complete搭建emacs的go语言自动补全功能

    上篇随笔记录了在emacs中使用go-mode和goflymake搭建了go语言的简单编程环境(推送门),今天来记录一下使用gocode+auto-complete配置emacs中go语言的自动补全功 ...