Leetcode 130. Surrounded Regions
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的更多相关文章
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- leetcode 130 Surrounded Regions(BFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 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 ...
- 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 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [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 ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- C语言:联合变量
#include <stdio.h> union hold{ int digit; double big; char letter; }; int main(){ union hold a ...
- JVM的堆(heap)、栈(stack)和方法区(method)
JVM主要由类加载器子系统.运行时数据区(内存空间).执行引擎以及与本地方法接口等组成.其中运行时数据区又由方法区Method Area.堆Heap.Java stack.PC寄存器.本地方法栈组成. ...
- AngularJS中的事件
欢迎大家指导与讨论 : ) 前言 Angular的作用域在本质上是分层次的(有的住户在低层, 有的住户在高层), 它们可以通过父子关系很自然地进行沟通.但通常, 这种沟通是单向的(父->子的单 ...
- Java7并发编程实战(一) 线程的管理
1:线程的创建 1:继承Thread类,并且覆盖run()方法 2:创建一个实现Runnable接口的类.使用带参数的Thread构造器来构造 2:example-->计算打印乘法表 首先 ...
- FineUI v4.0.2 (beta) 发布了!
FineUI v4.0.2 (beta) 已经于 2013-12-15 发布! ================================== 关于FineUI基于 ExtJS 的开源 ASP. ...
- ASP.NET 小白从零开始建站简易教程 (一)域名、虚拟主机、FTP上传文件
只考虑性价比,纯新手实验无备案.跟着步骤走半小时即可收获独立的个人网站一枚! 我的实验站 http://www.bearlab.site/ ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄ 目前总价花费86元(域名加虚 ...
- 网页上传图片 判断类型 检测大小 剪切图片 ASP.NET版本
本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=56&extra=page%3D1 我们在网页上传图片的时候,特 ...
- name after, name for, name as
name after, name for, name as name after是一个常见用法 : 1.Her parents named her Sophia after her grandmo ...
- offsetleft、offsetTop、offsetParent的兼容性问题
先来看看offsetParent返回的是什么值 ele.offsetParent返回的是ele元素最近的并且是定位过(relative,absolute)的父元素,如果没有父元素或者是父元素中没有一个 ...
- Alpha版本冲刺现场演示和阶段验收的总结
一共15个组.有13个组参加了今天的现场演示,分别是YZH.Radio Group.FZU5BOYS.静静看.Clean Code.Mod4.F4.For the Dream.Journey of C ...