【LeetCode OJ】Surrounded Regions
Problem Link:
http://oj.leetcode.com/problems/surrounded-regions/
We can do follows in the 2D board.
Use BFS from all 'O' cells on the boundary and mark all connected 'O' cells
Scan the board and flip all unmarked 'O' to 'X'.
The following code is python code accepted by oj.leetcode.com.
class Solution:
# @param board, a 2D array
# Capture all regions by modifying the input board in-place.
# Do not return any value.
def solve(self, board):
"""
Let m and n be the number of rows and columns of board
BFS from 'O' nodes on the boundary
"""
# Special case 1: if m <= 2, then all cells are on the boundary
m = len(board)
if m <= 2:
return
# Special case 2: if n <= 2, then all cells are on the boundary
n = len(board[0])
if n <= 2:
return # Queue used for BFS
Q = [] # Find all 'O' cells in on the boundary
# check the first row and last row
for j in xrange(n):
if board[0][j] == 'O':
Q.append( (0,j) )
if board[m-1][j] == 'O':
Q.append( (m-1,j) )
# check the first column and last column
for i in xrange(1,m-1):
if board[i][0] == 'O':
Q.append( (i,0) )
if board[i][n-1] == 'O':
Q.append( (i,n-1) ) # Start BFS from the nodes in Q
# Each time set the node to "M",
# which means the node is not captured
while Q:
new_Q = []
for (i,j) in Q:
board[i][j] = 'M'
# Check left node
if i > 0 and board[i-1][j] == 'O':
new_Q.append( (i-1,j) )
# Check right node
if i < m-1 and board[i+1][j] == 'O':
new_Q.append( (i+1,j) )
# Check top node
if j > 0 and board[i][j-1] == 'O':
new_Q.append( (i, j-1) )
# Check bottom node
if j < n-1 and board[i][j+1] == 'O':
new_Q.append( (i, j+1) )
Q = new_Q # Scan the 2D board
# 1) set node of O to X
# 2) set node of OO to O
for i in xrange(m):
for j in xrange(n):
if board[i][j] == 'O':
board[i][j] = 'X'
if board[i][j] == 'M':
board[i][j] = 'O'
【LeetCode OJ】Surrounded Regions的更多相关文章
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
随机推荐
- Vim经典讲解
http://blog.csdn.net/niushuai666/article/details/7275406
- 《Play for Java》学习笔记(二)基本的CRUD应用
注解: CRUD——Create,Retrieve, Update, Delete 文件结构
- div在固定高的文字垂直居中
<div style='display:table; height:100px;'> <div style='display:table-cell; vertical-align: ...
- mac 无法ssh localhost
mac 无法ssh localhost,错误提示:bash: /usr/local/bin/ssh_session: Permission denied在网上找了很久也没有找到解决方案,最后根据提示自 ...
- 克隆机器后eth1变为eth0问题
1. 清空该文件 2.进入网络配置文件把HADDR 和UUID注释掉,并重启 3.成功修改eth0 4. 4.可以结合这篇帖子来看 http://www.cnblogs.com/zydev/p/4 ...
- easy_install jinja2 mac linux
error: can't create or remove files in install directory The following error occurred while trying t ...
- if语句使用
package yuan; public class Yuan { public static void main(String[] args) { int a = 1; int b = 8; int ...
- 一模 (4) day1
第一题: 题目大意:给出N个人之间转账的手续X%,求出A转给B至少要多少钱才能使B得到100元.结果保留8位小数:N<=2000 解题过程: 1.很容易看出这题的图论模型,每条边的权值就是(1- ...
- 给出一个长度为n的数列,请对于每一个数,输出他右边第一个比他大的数。n<=100000.
RT,一个ppt里看到的题,不过没讲做法.百度上基本搜不到.自己想了个做法,理论上可行,复杂度也是O(nlogn). 首先,做一次RMQ,求区间最大值. 对于任意一个数s[i],可以用logn的时间求 ...
- Oracle性能调优
这部分目前主要是从网上搜集来的,后续要在实践中慢慢体会. v$sqltext: 存储的是被分割的sql v$sqlarea: 存储的是完整的sql和一些统计信息,比如累计的执行次数.逻辑读.物理读等( ...