leetcode-surrounded regions-ZZ
Problem Statement (link):
'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
X X X X
X X X X
X X X X
X O X X
Analysis:
Rather than recording the 2D positions for any scanned 'O', a trick is to substitute any border 'O's with another character - here in the Code I use 'Y'. And scan the board again to change any rest 'O's to 'X's, and change 'Y's back to 'O's.
We start searching 'O' from the four borders. I tried DFS first, the OJ gives Runtime error on the 250x250 large board; In the Sol 2 below, I implement BFS instead, and passed all tests.
The time complexity is O(n^2), as in the worst case, we may need to scan the entire board.
Code:
1, DFS
class Solution {
public:
// dfs - Runtime error on large board 250x250
void dfs(vector<vector<char>> &board, int r, int c) {
if (r<||r>board.size()-||c<||c>board[].size()-||board[r][c]!='O')
return;
board[r][c]='Y';
dfs(board, r-, c);
dfs(board, r+, c);
dfs(board, r, c-);
dfs(board, r, c+);
}
void solve(vector<vector<char>> &board) {
if (board.empty() || board.size()< || board[].size()<)
return;
int r=board.size();
int c=board[].size();
// dfs from boundary to inside
for (int i=; i<c; i++) {
if (board[][i]=='O')
dfs(board, , i); // first row
if (board[c-][i]=='O')
dfs(board, c-, i); // last row
}
for (int i=; i<board.size(); i++) {
if (board[i][]=='O')
dfs(board, i, ); // first col
if (board[i][c-])
dfs(board, i, c-); // last col
}
// scan entire matrix and set values
for (int i=; i<board.size(); i++) {
for (int j=; j<board[].size(); j++) {
if (board[i][j]=='O')
board[i][j]='X';
else if (board[i][j]=='Y')
board[i][j]='O';
}
}
}
};
2, BFS
class Solution {
public:
void solve(vector<vector<char>> &board) {
if (board.empty() || board.size()< || board[].size()<)
return;
int r=board.size();
int c=board[].size();
// queues to store row and col indices
queue<int> qr;
queue<int> qc;
// start from boundary
for (int i=; i<c; i++) {
if (board[][i]=='O') { qr.push(); qc.push(i); }
if (board[r-][i]=='O') { qr.push(r-); qc.push(i); }
}
for (int i=; i<r; i++) {
if (board[i][]=='O') { qr.push(i); qc.push(); }
if (board[i][c-]=='O') { qr.push(i); qc.push(c-); }
}
// BFS
while (!qr.empty()) {
int rt=qr.front(); qr.pop();
int ct=qc.front(); qc.pop();
board[rt][ct]='Y';
if (rt->= && board[rt-][ct]=='O') { qr.push(rt-); qc.push(ct); } //go up
if (rt+<r && board[rt+][ct]=='O') { qr.push(rt+); qc.push(ct); } // go down
if (ct->= && board[rt][ct-]=='O') { qr.push(rt); qc.push(ct-); } // go left
if (ct+<c && board[rt][ct+]=='O') { qr.push(rt); qc.push(ct+); } // go right
} // scan entire matrix and set values
for (int i=; i<board.size(); i++) {
for (int j=; j<board[].size(); j++) {
if (board[i][j]=='O') board[i][j]='X';
else if (board[i][j]=='Y') board[i][j]='O';
}
}
}
};
http://justcodings.blogspot.com/2014/07/leetcode-surrounded-regions.html
leetcode-surrounded regions-ZZ的更多相关文章
- [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
Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...
- 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 ...
随机推荐
- es put mapping
fd dg public Map<String, Map<String, String>> javaBeanToMapping(Object instance, List< ...
- CSAPP阅读笔记-struct, union, 数据对齐-来自第三章3.9的笔记-P183-P191
1.数据对齐 为什么要对齐:通俗点解释就是CPU对数据访问时,每次都是取固定数量的字节数,假如一次取4个字节,若有个int存在0x01-0x04,则一次就能取出,若存在0x03-0x06,则需要分两次 ...
- Failed to start NodeManager caused by "/var/lib/hadoop-yarn/yarn-nm-recovery/yarn-nm-state/LOCK: Permission denied"
Hadoop 安装步骤: 0. 安装前准备(节点机器,环境设置,yum源设置) 1. 配置并安装Cloudera-Manager 2. 启动 CM 服务 3. 安装CDH,并配置集群 4. 启动 ...
- (转)Mysql数据库管理 表的维护
原文:http://t.dbdao.com/archives/mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%AE%A1%E7%90%86-%E8%A1%A8%E7%9A%84 ...
- 关于table-layout的用法
定义:tableLayout 属性用来显示表格单元格.行.列的算法规则. 自动表格布局:auto(默认) 在自动表格布局中,列的宽度是由列单元格中没有折行的最宽的内容设定的. 此算法有时会较慢,这是由 ...
- JSP页面GET传值乱码问题
两个JSP页面进行GET传值的时候.两个页面的编码都是UTF-8,且传值之前设置response的content为UTF-8也解决不了问题. 设置tomcat的配置文件server.xml:在Conn ...
- Mongodb cassandra 和 Mysql对比
MongoDBDB.Cassandra和 Mysql对比 1.为什么是Nosql? 1.1 Nosql在大数据处理相对于关系型数据库具有优势 1.1.1 1. 低延迟 ...
- JS的正则表达式 - RegExp
RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 正则表达式的创建方式 1.文字格式,使用方法如下: /pattern/flags (即:/模式/标记) 2 ...
- Java入门系列-23-NIO(使用缓冲区和通道对文件操作)
NIO 是什么 java.nio全称java non-blocking(非阻塞) IO(实际上是 new io),是指jdk1.4 及以上版本里提供的新api(New IO) ,为所有的原始类型(bo ...
- 使用webgl(three.js)搭建一个3D智慧园区、3D建筑,3D消防模拟,web版3D,bim管理系统——第四课
序:这段时间忙于奔波,好久没有更新了,今天更新一下,继续上节课的完善讲解,算是对前段时间的一个总结吧.披星戴月的时光也算有点应用效果了. 对于webgl(three.js)性能这一块我在上节课< ...