LeetCode_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
solution: 思想是mark 出所有外围的O,里面的不管。
- First scan the four edges of the board, if you meet an 'O', call a recursive mark function to mark that region to something else (for example, '+');
- scan all the board, if you meet an 'O', flip it to 'X';
- scan again the board, if you meet an '+', flip it to 'O';
class Solution {
public:
void BFS(vector<vector<char>> &board, int i , int j){ if(board[i][j] == 'X' || board[i][j] == '#') return;
board[i][j] = '#'; if( i - >= ) BFS(board, i-, j );
if( i + < rows ) BFS(board, i+, j );
if( j - >= ) BFS(board, i, j- );
if( j + < columns ) BFS(board, i, j+ );
}
void solve(vector<vector<char>> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
rows = board.size();
if( rows == ) return ;
columns = board[].size();
if(columns == ) return ; for( int i = ; i < columns - ; i++)
{
BFS(board, ,i);//up
BFS(board, rows - , i );//down
}
for( int i = ; i< rows; i++)
{
BFS(board, i, ); //left
BFS(board, i, columns - ); //right
}
for(int i = ; i< rows; i++)
for(int j = ; j < columns ; j++)
{
if(board[i][j] == '#')
board[i][j] = 'O';
else if (board[i][j] == 'O')
board[i][j] = 'X';
}
}
private :
int rows;
int columns;
};
LeetCode_Surrounded Regions的更多相关文章
- [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
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 ...
- [REP]AWS Regions and Availability Zones: the simplest explanation you will ever find around
When it comes to Amazon Web Services, there are two concepts that are extremely important and spanni ...
- Leetcode 130. Surrounded Regions
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 ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- ural 1147. Shaping Regions
1147. Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) o ...
随机推荐
- 这样就算会了PHP么?-5
汇集一点关于数据深入一些的几个函数,去重,弹出,加入,查找.... <?php $str = "时装,休闲,职业装"; $strs = explode(",&quo ...
- TranslateAnimation详解
TranslateAnimation详解 Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, Tr ...
- Linux企业级项目实践之网络爬虫(24)——定制规则扩展为垂直爬虫
在垂直搜索的索引建立之前,我们需要到垂直网站上抓取资源并做一定的处理.垂直搜索与通用搜索不同之处在于,通用搜索不需要理会网站哪些资源是需要的,哪些是不需要的,一并抓取并将其文本部分做索引.而垂直搜索里 ...
- Jquery使用tbody编辑功能实现table输入计算功能
实例:编写一个输入计算(被减数-减数=差). HTML: <body> <table> <thead> <tr> <td >被减数</ ...
- poj3708:函数式化简+高精度进制转换+同余方程组
题目大意 给定一个函数 找出满足条件 等于 k 的最小的x m,k,d已知 其中 m,k 很大需要使用高精度存储 思路: 对 函数f(m)进行化简 ,令t=ceil( log(d,m) ) 可以得 ...
- 玩转指针(Playing with Pointers)
Question: What is a Pointer? What are its limitations? What are its benefits? How do we use it? What ...
- Exchange Server 2010/2013功能差异
- android图标设计事宜
1.Launcher图标 图标的最佳宽高是48x48 dp. ldpi:36*36px,0.75倍密度,一般不用提供,系统会从hdpi取图缩小1倍. mdpi:48*48px, 1倍密度 hdpi:7 ...
- URAL 1658
题目大意:求出T个最小的满足各个位的和为S1,平方和为S2的数.按顺序输出.数的位数大于100或者不存在这样一个数时,输出:No solution. KB 64bit IO Format:%I ...
- JAVA中运用数组的四种排序方法
JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法. 快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现. 冒泡法是运用遍历数组进 ...