【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions
Given a 2D board containing '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
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
核心思想:只有边界上'O'的位置组成的片区不会被'X'包围。
因此先对边界上的'O'遍历之后暂存为'*'。
非'*'的'O'即被'X'包围了。
解法一:DFS遍历
struct POS
{
int x;
int y;
POS(int newx, int newy): x(newx), y(newy) {}
}; class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty() || board[].empty())
return;
int m = board.size();
int n = board[].size();
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
{
if(i == || i == m- || j == || j == n-)
{// remain 'O' on the boundry
dfs(board, i, j, m, n);
}
}
}
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '*')
board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
stack<POS*> stk;
POS* pos = new POS(i, j);
stk.push(pos);
board[i][j] = '*';
while(!stk.empty())
{
POS* top = stk.top();
if(top->x > && board[top->x-][top->y] == 'O')
{
POS* up = new POS(top->x-, top->y);
stk.push(up);
board[up->x][up->y] = '*';
continue;
}
if(top->x < m- && board[top->x+][top->y] == 'O')
{
POS* down = new POS(top->x+, top->y);
stk.push(down);
board[down->x][down->y] = '*';
continue;
}
if(top->y > && board[top->x][top->y-] == 'O')
{
POS* left = new POS(top->x, top->y-);
stk.push(left);
board[left->x][left->y] = '*';
continue;
}
if(top->y < n- && board[top->x][top->y+] == 'O')
{
POS* right = new POS(top->x, top->y+);
stk.push(right);
board[right->x][right->y] = '*';
continue;
}
stk.pop();
}
}
};
解法二:BFS遍历
struct POS
{
int x;
int y;
POS(int newx, int newy): x(newx), y(newy) {}
}; class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty() || board[].empty())
return;
int m = board.size();
int n = board[].size();
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
{
if(i == || i == m- || j == || j == n-)
{// remain 'O' on the boundry
bfs(board, i, j, m, n);
}
}
}
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '*')
board[i][j] = 'O';
}
}
}
void bfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
queue<POS*> q;
board[i][j] = '*';
POS* pos = new POS(i, j);
q.push(pos);
while(!q.empty())
{
POS* front = q.front();
q.pop();
if(front->x > && board[front->x-][front->y] == 'O')
{
POS* up = new POS(front->x-, front->y);
q.push(up);
board[up->x][up->y] = '*';
}
if(front->x < m- && board[front->x+][front->y] == 'O')
{
POS* down = new POS(front->x+, front->y);
q.push(down);
board[down->x][down->y] = '*';
}
if(front->y > && board[front->x][front->y-] == 'O')
{
POS* left = new POS(front->x, front->y-);
q.push(left);
board[left->x][left->y] = '*';
}
if(front->y < n- && board[front->x][front->y+] == 'O')
{
POS* right = new POS(front->x, front->y+);
q.push(right);
board[right->x][right->y] = '*';
}
}
}
};
这边再给一种递归实现的dfs供参考,简洁很多,但是在leetcode中由于栈溢出会显示Runtime Error
void dfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
board[i][j] = '*';
if(i > && board[i-][j] == 'O') // up
dfs(board, i-, j, m, n);
if(i < m- && board[i+][j] == 'O') // down
dfs(board, i+, j, m, n);
if(j > && board[i][j-] == 'O') // left
dfs(board, i, j-, m, n);
if(j < n- && board[i][j+] == 'O') // right
dfs(board, i, j+, m, n);
}
【LeetCode】130. Surrounded Regions (2 solutions)的更多相关文章
- 【一天一道LeetCode】#130. Surrounded Regions
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【LeetCode】338. Counting Bits (2 solutions)
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode】242. Valid Anagram (2 solutions)
Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- 【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
随机推荐
- C#常使用的正则表达式
/// <summary> /// 是否为手机号码 /// </summary> /// <param name="value"></pa ...
- nginx配置location总结
location匹配顺序 "="前缀指令匹配,如果匹配成功,则停止其他匹配 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配) ...
- 用Python语言写Hadoop MapReduce程序Writing an Hadoop MapReduce Program in Python
In this tutorial I will describe how to write a simple MapReduce program for Hadoop in the Python pr ...
- 端口复用技术简单了解;重用端口;socket复用端口
端口复用相关点 多个应用复用端口,只有最后一个绑定的socket可以接受数据,所有socket都可以发送数据 使用端口复用技术时,所有的socket都开启端口复用,才可以实现端口复用 黑客技术,使用标 ...
- 不错网络性能相关的文章-BaiduRPC
http://wiki.baidu.com/display/RPC/Threading+Overview#ThreadingOverview-单线程reactor Threading Overview ...
- 使用Topshelf 5步创建Windows 服务
使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with T ...
- Remove Duplicates from Sorted List leetcode java
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- 【ES】elasticsearch学习笔记
ES学习 1 优势 1.1 简单 1.1.1 相比Solor配置部署等非常简单 1.2 高效 1.2.1 ES使用Netty作为内部RPC框架,Solor使用Jetty 1.3 插件化 1.3.1 E ...
- Compiling LIBFFM On OSX 10.9
原文:http://blog.josephmisiti.com/compiling-libffm-on-osx-10.9/ I recently tried to compile LIBFFM (Fi ...
- 【Nodejs】cheerio简单示例
cheerio的API挺多,我也了解有限,欲知详情请参考 “通读cheerio API”. 下面就事论事聊聊它的基本使用. 比如说在某网页中有这么一段HTML: </tbody> < ...