leetcode — surrounded-regions
import java.util.Arrays;
import java.util.Stack;
/**
* Source : https://oj.leetcode.com/problems/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 O O X
* X X X X
* X O X X
* X X O 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
*/
public class SurroundedRegions {
/**
* 将所有被X包围的O替换为X
* 先找出不被包围的O替换为Y
* 然后将所有剩下的所有的O替换为X
* 最后将所有的Y替换为O
*
* 其中关键是将不被包围的O替换为Y,也就是要找出所有不被包围的O
* 从边缘开始,如果边缘处的元素为O则寻找其周围是否有为O的元素,直到没有O或者没有元素
*
*
* @param board
* @return
*/
public char[][] solve (char[][] board) {
if (board.length == 0) {
return board;
}
fillBoard(board, 'O', 'Y');
replace(board, 'O', 'X');
fillBoard(board, 'Y', 'O');
return board;
}
/**
* 将board中的指定元素替换为另外一个
*
* @param board
* @param source
* @param target
*/
private void fillBoard (char[][] board, char source, char target) {
for (int i = 0; i < board.length; i++) {
fill(board, i, 0, source, target);
fill(board, i, board[0].length-1, source, target);
}
for (int i = 0; i < board[0].length; i++) {
fill(board, 0, i, source, target);
fill(board, board.length-1, i, source, target);
}
}
private void fill (char[][] board, int i, int j, char source, char target) {
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != source) {
return;
}
Stack<Position> stack = new Stack<Position>();
stack.push(new Position(i, j));
while (stack.size() > 0) {
Position position = stack.pop();
board[position.i][position.j] = target;
if (position.i > 0 && board[position.i-1][position.j] == source) {
stack.push(new Position(position.i-1, position.j));
}
if (position.i < board.length-1 && board[position.i+1][position.j] == source) {
stack.push(new Position(position.i+1, position.j));
}
if (position.j > 0 && board[position.i][position.j-1] == source) {
stack.push(new Position(position.i, position.j-1));
}
if (position.j < board[0].length-1 && board[position.i][position.j+1] == source) {
stack.push(new Position(position.i, position.j+1));
}
}
}
/**
* 将source替换为target
*
* @param board
* @param source
* @param target
*/
private void replace (char[][] board, char source, char target) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == source) {
board[i][j] = target;
}
}
}
}
private static void print (char[][] board) {
for (int i = 0; i < board.length; i++) {
System.out.println(Arrays.toString(board[i]));
}
System.out.println();
}
private class Position {
int i;
int j;
public Position(int i, int j) {
this.i = i;
this.j = j;
}
}
public static void main(String[] args) {
SurroundedRegions surroundedRegions = new SurroundedRegions();
char[][] board = {
{'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X'},
{'X', 'X', 'O', 'X'}
};
print(surroundedRegions.solve(board));
}
}
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 解题报告
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- 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 ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- [LeetCode] Surrounded Regions 广度搜索
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- hdu1814 Peaceful Commission
hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...
- go web开发(gin&gorm) 之DB配置及DAO的基本使用
转载请注明出处: https://www.cnblogs.com/funnyzpc/p/9501376.html ``` 我先闲扯下,前天(也就是2018年11月16号)的某个时候,忽然有人在QQ ...
- ubuntu的安装及ubuntu中安装mysql和tomcat
一.安装ubuntu 1.创建虚拟机 2.向导选择自定义 3.然后下一步再下一步,直到这里,稍后再安装系统 4.然后选择linux,注意这里下面的下拉选择Ubuntu64,因为我们下载的是64位的,如 ...
- CentOS7 VMware-Tools安装与共享文件夹设置
一. VMware-Tools安装 1.加载VMware Tools的光驱:点击"虚拟机"->"安装VMware Tools".这里,由于我已经安装了,所 ...
- 加固后,上传play store, 在 google play store 下载应用安装后,打开签名校验失败
在Google Play Console. (Google Play App Signing )签署您的应用 在创建应用时: 会有个“ Google Play App Signing” 的东西,提示使 ...
- tf.contrib.slim.data数据加载(1) reader
reader: 适用于原始数据数据形式的Tensorflow Reader 在库中parallel_reader.py是与reader相关的,它使用多个reader并行处理来提高速度,但文件中定义的类 ...
- Caused by: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, ...}
解决办法:查看与该项目中的所有#{},应该是 #{}的中间没有写值
- php位运算
php位运算 /** * 位运算 */ echo "<pre>"; $aa = $a&$b; //按位与,相同位都为1时为1,其他都为0; echo " ...
- vue.js数据可以在页面上渲染成功却总是警告提示某个字段“undefined”未定义
最近在开发公司的一个后端管理系统,用的是比较流行的vue框架.在开发过程中,总是出现各种各样的报错问题,有警告的,有接口不通的,有自己马虎造成的低级错误的等等,这些错误在一些老司机面前分分钟解决,但今 ...
- DataRow[]
datarow[]是datatable 的行数组集合,但是呢好像是不能直接初始化,只能是datarow[] rows = dt.select(condition);这样子.(也可能我才疏学浅没找到吧) ...