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 ...
随机推荐
- [Linux] 使用Yum在CentOS上安装MySQL
跟随官网上的安装教程:https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html官网上还有一个QuickGuide ...
- NOI2017总结
时光剥离你我像一袭华美衣衫 却要被追悔爬满 退役之战,去得匆匆,看得蒙蒙. 第三次全国赛,曾经的APIO初二选手也走到了时间的尽头. 第一次走向全国舞台的激动与忐忑,第一次在大赛中失利的沮丧与绝望,第 ...
- [CF1093E]Intersection of Permutations
[CF1093E]Intersection of Permutations 题目大意: 给定两个长度为\(n(n\le2\times10^5)\)的排列\(A,B\).\(m(m\le2\times1 ...
- CTF最简单的Web题
http://www.shiyanbar.com/ctf/1810 天网管理系统天网你敢来挑战嘛格式:ctf{ }解题链接: http://ctf5.shiyanbar.com/10/web1 查看源 ...
- 类型后面加问号 int?
类型后面加问号 int? 单问号---用于给变量设初值的时候,给变量(int类型)赋值为null,而不是0! 双问号---用于判断并赋值,先判断当前变量是否为null,如果是就可以赋一个新值,否则跳过 ...
- 理解pytorch中的softmax中的dim参数
import torch import torch.nn.functional as F x1= torch.Tensor( [ [1,2,3,4],[1,3,4,5],[3,4,5,6]]) y11 ...
- hashlib模块,shutil,模块 ,,xml 文件解析,configparser,模块,类,什么是类
1 什么是hash hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值 如果把hash算法比喻为一座工厂 那传给hash算法的内容就是原材料 生成的hash值就是生产出的产品 2.为 ...
- SVM支持向量机 详解(含公式推导)
关于SVM的内容,这三位老哥写的都挺好的,内容是互补的,结合他们三位的一起看,就可以依次推导出SVM得公式了. https://www.cnblogs.com/steven-yang/p/565836 ...
- node03
1.express处理post请求 借助body-parse中间件,其实最终我们也不会使用这个 对于get请求,无需中间件,用req.query即可返回相应的数据 但是post我们尝试借助中间件处理 ...
- 4.再来看看逆向——OD的简介
目录 1.前言 2.一些设置和配置 3.开始了解OD 代码窗口 数据窗口 小端序问题 前言 前3节主要写了恶意代码用到的手段,接下来先写一下关于逆向调试的一些内容.毕竟逆向比较难理解一点. 一些配置和 ...