[leetcode]130. Surrounded Regions
来源:互联网 发布:巨人网络收购蓝洞 编辑:程序博客网 时间:2024/05/16 03:42
题目链接:https://leetcode.com/problems/surrounded-regions/#/description
Given a 2D board containing 'X'
and 'O'
(the letter 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 XX O O XX X O XX O X X
After running your function, the board should be:
X X X XX X X XX X X XX O X X
- First, check the four border of the matrix. If there is a element is
'O', alter it and all its neighbor 'O' elements to '1'.
- Then ,alter all the 'O' to 'X'
- At last,alter all the '1' to 'O'
For example:
X X X X X X X X X X X X X X O X -> X X O X -> X X X X X O X X X 1 X X X O X X X O X X X 1 X X X O X X
class Solution {public: void solve(vector<vector<char>>& board) { int i,j; int row=board.size(); if(!row) return; int col=board[0].size(); for(i=0;i<row;i++) { check(board,i,0,row,col); if(col>1) check(board,i,col-1,row,col); } for(j=1;j+1<col;j++) { check(board,0,j,row,col); if(row>1) check(board,row-1,j,row,col); } for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(board[i][j]=='O') board[i][j]='X'; } } for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(board[i][j]=='1') board[i][j]='O'; } } } void check(vector<vector<char>>& vec,int i,int j,int row,int col) { if(vec[i][j]=='O') { vec[i][j]='1'; if(i>1) check(vec,i-1,j,row,col); if(j>1) check(vec,i,j-1,row,col); if(i+1<row) check(vec,i+1,j,row,col); if(j+1<col) check(vec,i,j+1,row,col); } }};
阅读全文
0 0
- [LeetCode]130.Surrounded Regions
- LeetCode 130. Surrounded Regions
- [leetcode] 130.Surrounded Regions
- Leetcode 130. Surrounded Regions
- leetcode 130. Surrounded Regions
- leetcode 130. Surrounded Regions
- LeetCode 130. Surrounded Regions
- [LeetCode]130. Surrounded Regions
- LeetCode 130. Surrounded Regions
- LeetCode *** 130. Surrounded Regions
- Leetcode 130. Surrounded Regions
- [leetcode] 130. Surrounded Regions
- LeetCode-130.Surrounded Regions
- Leetcode 130. Surrounded Regions
- [LeetCode] 130. Surrounded Regions
- leetcode-- 130. Surrounded Regions
- 【LeetCode】130. Surrounded Regions
- Leetcode-130. Surrounded Regions
- Koltin简明学习,also,takeIf,takeUnless
- 剑指Offer——丑数
- 正则表达式中各种字符的含义
- gh0st源码分析与远控的编写(三)
- Kali Linux 使用教程
- [leetcode]130. Surrounded Regions
- Android make清除命令
- java学习笔记(三十六)JDBC(二)
- InputStream到String只差一个Scanner
- Android 自定义控件之滚动字幕条
- lintcode python 代码 133 最长单词
- 椭圆曲线密码学简介****
- css float
- eclipse配置安卓开发环境之安装Android SDK失败解决方案