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 ...
随机推荐
- BZOJ2143: 飞飞侠
2143: 飞飞侠 题意: 给出两个 n ∗ m 的矩阵 A,B,以及 3 个人的坐标 在 (i, j) 支付 Ai,j 的费用可以弹射到曼哈顿距离不超过 Bi,j 的位置 问三个人汇合所需要的最小总 ...
- python学习:continue及break使用
continue及break使用 #continue 作用:结束本次循环,继续下次循环#break 作用:跳出整个当次循环 for i in range(10): if i < 5: conti ...
- 关于部署php遇到的坑
业务突然要启动一个久不使用的PHP项目, 发现部署到centos7上后 各种报错 就是不行. 我怀疑是apache或者php问题 就重新安装 编译安装也试过就是不行. 只能按笨办法 在测试环境安装了a ...
- pyhton 监听文件输入实例
def tail(filename): f = open(filename,encoding='utf-8') while True: line = f.readline() if line.stri ...
- 4.DHCP与PRE
如何配置IP地址 使用net-tools $ sudo ifconfig eth1 10.0.0.1/24 $ sudo ifconfig eth1 up 使用Iproute2 ...
- 操作XML
别人已经写过很好的XML辅助类,可以直接引用后使用: 我这里自己写一个xml的操作类,目前能实现的是对一个不含集合的类可以操作,含集合的类无法将集合里的数据读取出来, 首先定义一个XML特性,用于区分 ...
- JAVA高性能I/O设计模式
Java中的IO方式 主要分为3种:BIO(同步阻塞).NIO(同步非阻塞)和AIO(异步非阻塞). BIO 同步阻塞模式.在JDK1.4以前,使用Java建立网络连接时,只能采用BIO方式,在服务器 ...
- 深入理解Spring Redis的使用 (四)、RedisTemplate执行Redis脚本
对于Redis脚本使用过的同学都知道,这个主要是为了防止竞态条件而用的.因为脚本是顺序执行的.(不用担心效率问题)比如我在工作用,用来设置考试最高分. 如果还没有用过的话,先去看Redis脚本的介绍, ...
- Spring的核心接口
ContextLoaderListener接口 Create a new ContextLoaderListenerthat will create a web application context ...
- [Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays
Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping ...