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的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  3. LeetCode: Surrounded Regions 解题报告

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  4. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  5. Leetcode: Surrounded regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  6. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

  7. LeetCode: Surrounded Regions [130]

    [题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...

  8. [LeetCode] Surrounded Regions 广度搜索

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  9. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  10. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. System.getProperty(String key)方法获取常用系统信息

    其中key可以为以下选项: 1.java.version Java 运行时环境版本 2.java.vendor Java 运行时环境供应商 3.java.vendor.url Java 供应商的 UR ...

  2. ionic3+angular4开发混合app 之自定义组件

    这里主要是记录ionic3+angular4开发混合app时自定义组件,我想自定义组件的方法和angular4应该类似,具体在纯angular4中自定义组件,暂时没有实践,个人觉得差别不大,之后实践了 ...

  3. 大数据计算框架Hadoop, Spark和MPI

    转自:https://www.cnblogs.com/reed/p/7730338.html 今天做题,其中一道是 请简要描述一下Hadoop, Spark, MPI三种计算框架的特点以及分别适用于什 ...

  4. 对于Python turtle的学习笔记

    进一步地,我尝试学习了Python 的其中一个非常重要的函数库——turtle库 这是一个用于python绘图的函数库,方便又好用! 对于它的安装,现在我们所用的python 3的系统运用到的指令是: ...

  5. CSS Sprite雪碧图

    为了减少http请求数量,加速网页内容显示,很多网站的导航栏图标.登录框图片等,使用的并不是<image>标签,而是CSS Sprite雪碧图. 两个小例子: 淘宝首页的侧栏图 代码 &l ...

  6. atx-agent minicap、minitouch源码分析

    项目描述: 因为公司需要,特别研究了一下openatx系列手机群控源码 源码地址: https://github.com/openatx 该项目主要以go语言来编写服务端.集成 OpenSTF中核心组 ...

  7. mysql uodate 报错 You can't specify target table '**' for update in FROM clause

    You can't specify target table 'sc' for update in FROM clause 背景:把“sc”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩: 上面 ...

  8. vs2013新建asp.net web 项目报错,此模板尝试加载组件程序集NuGet Package Manage

    打开vs2013,工具->扩展和更新->联机->找到NuGet Package Manager->安装->重新启动vs2013

  9. ubuntu Pycharm 2017 3.3 Active

    1.打开激活窗口 2.选择 Activate new license with License server (用license server 激活) 3.在 License sever addres ...

  10. Dora.Interception,为.NET Core度身打造的AOP框架 [5]:轻松地实现与其他AOP框架的整合

    这里所谓的与第三方AOP框架的整合不是说改变Dora.Interception现有的编程,而是恰好相反,即在不改变现有编程模式下采用第三方AOP框架或者自行实现的拦截机制.虽然我们默认提供基于IL E ...