1. import java.util.Arrays;
  2. import java.util.Stack;
  3. /**
  4. * Source : https://oj.leetcode.com/problems/surrounded-regions/
  5. *
  6. *
  7. * Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
  8. *
  9. * A region is captured by flipping all 'O's into 'X's in that surrounded region.
  10. *
  11. * For example,
  12. *
  13. * X O O X
  14. * X X X X
  15. * X O X X
  16. * X X O X
  17. *
  18. * After running your function, the board should be:
  19. *
  20. * X X X X
  21. * X X X X
  22. * X X X X
  23. * X O X X
  24. */
  25. public class SurroundedRegions {
  26. /**
  27. * 将所有被X包围的O替换为X
  28. * 先找出不被包围的O替换为Y
  29. * 然后将所有剩下的所有的O替换为X
  30. * 最后将所有的Y替换为O
  31. *
  32. * 其中关键是将不被包围的O替换为Y,也就是要找出所有不被包围的O
  33. * 从边缘开始,如果边缘处的元素为O则寻找其周围是否有为O的元素,直到没有O或者没有元素
  34. *
  35. *
  36. * @param board
  37. * @return
  38. */
  39. public char[][] solve (char[][] board) {
  40. if (board.length == 0) {
  41. return board;
  42. }
  43. fillBoard(board, 'O', 'Y');
  44. replace(board, 'O', 'X');
  45. fillBoard(board, 'Y', 'O');
  46. return board;
  47. }
  48. /**
  49. * 将board中的指定元素替换为另外一个
  50. *
  51. * @param board
  52. * @param source
  53. * @param target
  54. */
  55. private void fillBoard (char[][] board, char source, char target) {
  56. for (int i = 0; i < board.length; i++) {
  57. fill(board, i, 0, source, target);
  58. fill(board, i, board[0].length-1, source, target);
  59. }
  60. for (int i = 0; i < board[0].length; i++) {
  61. fill(board, 0, i, source, target);
  62. fill(board, board.length-1, i, source, target);
  63. }
  64. }
  65. private void fill (char[][] board, int i, int j, char source, char target) {
  66. if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != source) {
  67. return;
  68. }
  69. Stack<Position> stack = new Stack<Position>();
  70. stack.push(new Position(i, j));
  71. while (stack.size() > 0) {
  72. Position position = stack.pop();
  73. board[position.i][position.j] = target;
  74. if (position.i > 0 && board[position.i-1][position.j] == source) {
  75. stack.push(new Position(position.i-1, position.j));
  76. }
  77. if (position.i < board.length-1 && board[position.i+1][position.j] == source) {
  78. stack.push(new Position(position.i+1, position.j));
  79. }
  80. if (position.j > 0 && board[position.i][position.j-1] == source) {
  81. stack.push(new Position(position.i, position.j-1));
  82. }
  83. if (position.j < board[0].length-1 && board[position.i][position.j+1] == source) {
  84. stack.push(new Position(position.i, position.j+1));
  85. }
  86. }
  87. }
  88. /**
  89. * 将source替换为target
  90. *
  91. * @param board
  92. * @param source
  93. * @param target
  94. */
  95. private void replace (char[][] board, char source, char target) {
  96. for (int i = 0; i < board.length; i++) {
  97. for (int j = 0; j < board[0].length; j++) {
  98. if (board[i][j] == source) {
  99. board[i][j] = target;
  100. }
  101. }
  102. }
  103. }
  104. private static void print (char[][] board) {
  105. for (int i = 0; i < board.length; i++) {
  106. System.out.println(Arrays.toString(board[i]));
  107. }
  108. System.out.println();
  109. }
  110. private class Position {
  111. int i;
  112. int j;
  113. public Position(int i, int j) {
  114. this.i = i;
  115. this.j = j;
  116. }
  117. }
  118. public static void main(String[] args) {
  119. SurroundedRegions surroundedRegions = new SurroundedRegions();
  120. char[][] board = {
  121. {'X', 'X', 'X', 'X'},
  122. {'X', 'X', 'X', 'X'},
  123. {'X', 'O', 'X', 'X'},
  124. {'X', 'X', 'O', 'X'}
  125. };
  126. print(surroundedRegions.solve(board));
  127. }
  128. }

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. BZOJ2143: 飞飞侠

    2143: 飞飞侠 题意: 给出两个 n ∗ m 的矩阵 A,B,以及 3 个人的坐标 在 (i, j) 支付 Ai,j 的费用可以弹射到曼哈顿距离不超过 Bi,j 的位置 问三个人汇合所需要的最小总 ...

  2. python学习:continue及break使用

    continue及break使用 #continue 作用:结束本次循环,继续下次循环#break 作用:跳出整个当次循环 for i in range(10): if i < 5: conti ...

  3. 关于部署php遇到的坑

    业务突然要启动一个久不使用的PHP项目, 发现部署到centos7上后 各种报错 就是不行. 我怀疑是apache或者php问题 就重新安装 编译安装也试过就是不行. 只能按笨办法 在测试环境安装了a ...

  4. pyhton 监听文件输入实例

    def tail(filename): f = open(filename,encoding='utf-8') while True: line = f.readline() if line.stri ...

  5. 4.DHCP与PRE

    如何配置IP地址 使用net-tools      $ sudo ifconfig eth1 10.0.0.1/24      $ sudo ifconfig eth1 up   使用Iproute2 ...

  6. 操作XML

    别人已经写过很好的XML辅助类,可以直接引用后使用: 我这里自己写一个xml的操作类,目前能实现的是对一个不含集合的类可以操作,含集合的类无法将集合里的数据读取出来, 首先定义一个XML特性,用于区分 ...

  7. JAVA高性能I/O设计模式

    Java中的IO方式 主要分为3种:BIO(同步阻塞).NIO(同步非阻塞)和AIO(异步非阻塞). BIO 同步阻塞模式.在JDK1.4以前,使用Java建立网络连接时,只能采用BIO方式,在服务器 ...

  8. 深入理解Spring Redis的使用 (四)、RedisTemplate执行Redis脚本

    对于Redis脚本使用过的同学都知道,这个主要是为了防止竞态条件而用的.因为脚本是顺序执行的.(不用担心效率问题)比如我在工作用,用来设置考试最高分. 如果还没有用过的话,先去看Redis脚本的介绍, ...

  9. Spring的核心接口

    ContextLoaderListener接口 Create a new ContextLoaderListenerthat will create a web application context ...

  10. [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 ...