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 ...
随机推荐
- linux的yum报错
yum makecache Loaded plugins: fastestmirror Could not retrieve mirrorlist http://mirrorlist.centos.o ...
- java NIO学前准备
之前一直对NIO感兴趣,无奈对IO的很多概念很模糊,所以对于NIO的学习也是一直半解,最近在网上查阅了很多资料,发现有很多概念是需要反复理解的,有的时候甚至当时理解了,但一段时间后又忘记了,所以决定自 ...
- 导入数据到HBase的方式选择
Choosing the Right Import Method If the data is already in an HBase table: To move the data from one ...
- Request笔记
1 Request 的简介和运行环境 1.HttpServletRequest 概述 我们在创建 Servlet 时会覆盖 service()方法,或 doGet()/doPost(),这些方法都有两 ...
- orcale 之PL/SQL 控制语句
控制语句是PL/SQL 的关键所在.只有学好这些控制语句才能在工作中更好的实现各种的功能. 选择结构 1. IF 语句 和其他的编程语言很类似.它的具体机构如下: IF(条件)THEN {语句} EL ...
- 设计模式之第7章-外观模式(Java实现)
设计模式之第7章-外观模式(Java实现) “鱼哥,知道怎么把大象装进冰箱里面么?”(作者按:这么简单的问题还想考我,早了几百年吧.)“把大象装进冰箱里,一共需要三步:第一步,把冰箱门打开:第二步,把 ...
- Hibernate中的CRUD操作
1.添加数据操作 插入数据使用session对象的save()方法完成. 插入代码: @Test public void Test1(){ SessionFactory sessionFactor ...
- lighttpd 与 gitweb 搭建服务器
搭建 Git 仓库服务器 下载 gitweb 如果是用 debian 系的 Linux 发行版,可以使用 apt 下载安装可执行的 gitweb sudo apt-get install gitweb ...
- [转]真正了解CSS3背景下的@font face规则
本文转自:http://www.zhangxinxu.com/wordpress/2017/03/css3-font-face-src-local/ by zhangxinxu from http:/ ...
- RUP 4+1视图
RUP开篇之作:http://www.ibm.com/developerworks/cn/rational/r-4p1-view/index.html 百科:http://baike.baidu.co ...