leetcode 128: Surrounded Regions
来源:互联网 发布:知我药妆芙丽芳丝 编辑:程序博客网 时间:2024/04/30 06:36
Surrounded RegionsFeb 22976 / 3801
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 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
java:
class Solution { public void solve(char[][] board) { if(board==null || board.length<=2) return; if(board[0]==null || board[0].length<=2) return; int m = board.length; int n = board[0].length; Queue<Pair> queue = new LinkedList<Pair>(); for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(board[i][j] == 'O') { if(i==0 || i==m-1 || j==0 || j==n-1) { queue.offer( new Pair(i,j) ); } else board[i][j] = 'P'; } } } while(!queue.isEmpty() ) { Pair p = queue.poll(); int x = p.x; int y = p.y; if( x-1>0 && board[x-1][y] =='P' ) { board[x-1][y] = 'O'; queue.offer( new Pair(x-1,y) ); } if( x+1<m && board[x+1][y] =='P' ) { board[x+1][y] = 'O'; queue.offer( new Pair(x+1,y) ); } if( y-1>0 && board[x][y-1] =='P' ) { board[x][y-1] = 'O'; queue.offer( new Pair(x,y-1) ); } if( y+1<n && board[x][y+1] =='P' ) { board[x][y+1] = 'O'; queue.offer( new Pair(x,y+1) ); } } for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(board[i][j] == 'P') board[i][j] = 'X'; } } } class Pair{ int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } }}
c++:
class Solution {public: void solve(vector<vector<char>> &board) { // Start typing your C/C++ solution below // DO NOT write int main() function int m = board.size(); if(m<1) return; int n = board[0].size(); queue<pair<int,int>> que; set<pair<int,int>> unique; for(int i=0; i<n; i++) { if(board[0][i]=='O') { board[0][i] = 'S'; que.push( make_pair(0,i) ); } if(board[m-1][i]=='O') { board[m-1][i] = 'S'; que.push( make_pair(m-1,i) ); } } for(int i=1; i<m-1; i++) { if(board[i][0]=='O') { board[i][0] = 'S'; que.push( make_pair(i,0) ); } if(board[i][n-1]=='O') { board[i][n-1] = 'S'; que.push( make_pair(i, n-1) ); } } while(!que.empty()){ pair<int,int> p = que.front(); que.pop(); for(int i=-1; i<=1; i++) { for(int j=-1; j<=1; j++) { if(p.first+i<0 || p.first+i>m-1 || p.second+j<0 || p.second+j>n-1 || (abs(i+j)!=1) ) continue; char & c = board[p.first+i][p.second+j]; pair<int,int> index = make_pair(p.first+i, p.second+j); if(c=='O' && unique.find(index) == unique.end() ) { que.push(index); unique.insert(index); c = 'S'; } } } } for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(board[i][j] == 'S') { board[i][j] = 'O'; } else if( board[i][j] == 'O') { board[i][j] = 'X'; } } } }};
- leetcode 128: Surrounded Regions
- LeetCode : Surrounded Regions
- [leetcode] Surrounded Regions
- LeetCode 39: Surrounded Regions
- 【leetcode】Surrounded Regions
- LeetCode - Surrounded Regions
- LeetCode之Surrounded Regions
- leetcode:Surrounded Regions
- [LeetCode]Surrounded Regions
- LeetCode - Surrounded Regions
- [Leetcode]Surrounded Regions
- Leetcode Surrounded Regions
- [leetcode]Surrounded Regions
- leetcode Surrounded Regions
- Leetcode: Surrounded Regions
- LeetCode-Surrounded Regions
- [leetcode] Surrounded Regions
- leetcode Surrounded Regions 详解
- SOA自学之路
- 在where中使用decode函数
- 如何查看本机电脑flash的版本
- Deep Learning(深度学习)学习笔记整理系列之(七)
- UNDO表空间
- leetcode 128: Surrounded Regions
- opencv-模板匹配
- session与会话cookie
- 0030算法笔记——【回溯法】最大团问题和图的m着色问题
- 常用工具
- CString转为unsigned char
- 基于Ruby语言的Web自动化测试框架LazyMan安装介绍
- ABAP SY/SYST 字段/描述对照
- 在sysservers中找不到服务器