[Lintcode]Surrounded Regions
来源:互联网 发布:微信朋友圈的网络骗局 编辑:程序博客网 时间:2024/06/01 10:11
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.
X X X XX O O XX X O XX O X X
After capture all regions surrounded by 'X'
, the board should be:
X X X XX X X XX X X XX O X X
public class Solution { /** * @param board a 2D board containing 'X' and 'O' * @return void */ public void surroundedRegions(char[][] board) { if(board.length == 0) return; if(board[0].length == 0) return; for(int i = 0; i < board.length; i++) { if(board[i][0] == 'O') { fill(board, i, 0); } if(board[i][board[0].length - 1] == 'O') { fill(board, i, board[0].length - 1); } } for(int i = 0; i < board[0].length; i++) { if(board[0][i] == 'O') { fill(board, 0, i); } if(board[board.length - 1][i] == 'O') { fill(board, board.length - 1, i); } } for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[0].length; j++) { if(board[i][j] == 'O') board[i][j] = 'X'; if(board[i][j] == 'F') board[i][j] = 'O'; } } } void fill(char[][] board, int x, int y) { if(x < 0 || x >= board.length || y < 0 || y >= board[0].length) return; if(board[x][y] != 'O') return; board[x][y] = 'F'; fill(board, x + 1, y); fill(board, x - 1, y); fill(board, x, y - 1); fill(board, x, y + 1); }}
0 0
- [Lintcode]Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- linux线程传参数
- 精心整理的操作系统相关概念
- linearlayout2
- bootstrap 使用row时出现的左右方向滚动条
- 向awk传递参数
- [Lintcode]Surrounded Regions
- HTML5 本地存储 localStorage、sessionStorage 的遍历、存储大小限制处理
- Python实现下载ftp图片
- Effective Java读书笔记一(Java Tips.Day.1)
- OSX 下安装php7
- vsftpd安装和使用
- java笔试题集1
- 05
- 例题5-3 安迪的第一个字典 UVa10815