Leetcode: Surrounded Regions
来源:互联网 发布:化工过程模拟软件 编辑:程序博客网 时间:2024/04/28 18:34
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
public class Solution { public void solve(char[][] board) {// Start typing your Java solution below// DO NOT write main() functionif(board.length < 1)return;//change 'O' to 'A'//left -> rightfor(int j = 0; j < board[0].length; j++)flip(board, 0, j);//up -> downfor(int i = 0; i < board.length; i++)flip(board, i, board[0].length - 1);//right -> leftfor(int j = board[0].length - 1; j >= 0; j--)flip(board, board.length - 1, j);//down -> upfor(int i = board.length - 1; i >= 0; i--)flip(board, i, 0);//change 'O' to 'X'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';//change 'A' to 'O'for(int i = 0; i < board.length; i++)for(int j = 0; j < board[0].length; j++)if(board[i][j] == 'A')board[i][j] = 'O';}public void flip(char[][] board, int i, int j){if(i < 0 || i >= board.length || j < 0 || j >= board[0].length)return;if(board[i][j] == 'O'){board[i][j] = 'A';flip(board, i - 1, j);flip(board, i + 1, j);flip(board, i, j - 1);flip(board, i, j + 1);}}}
- LeetCode : Surrounded Regions
- [leetcode] Surrounded Regions
- leetcode 128: 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 详解
- C#中GridView控件的使用
- Linux curl post data
- 武功·小山怡情
- 升级glibc
- cocos2d 引入zlib (for android)
- Leetcode: Surrounded Regions
- poj3481-Double Queue
- [Linux相关] img类型的固件如何解压
- WSAStartup
- JSP页面跳转大全
- 北京或成“下一个硅谷” 李开复扮演重要角色(Beijing’s Great Leap Forward)
- const的那些小事
- 武功·千亩草甸
- Android程序检测网络是否可用