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 X X X
X O O X
X X O X
X O X 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

题目大意:给一个棋盘,上面有'X'和'O',找出所有被'X'包围的'O',并替换成'X'。

解题思路:

解法一:从边开始,找出在四条边上的O,以BFS的方式找出所有与边上的O连通的O,这些O都是不被包围的,将这些O替换为某个特殊字符,遍历完之后,将O替换为X,将特殊字符替换为O。

解法二:直接用BFS遍历,找到一个O之后,加入BFS队列,找出与之连通的所有的O同时判断这些O是否被包围(即是否位于某条边上),将这些O加入一个List,每一个连通的O区域用一个surround布尔变量表示这个连通区域是否是被surround的,如果是就把这些O替换为X,否则不替换并继续遍历其他。

注意:这里因为要把i、j下标放入队列或者结果集,一开始我使用String类型key= i+"_"+j 来处理,后来看到别人key = i * colLen + j ; colLen是棋盘的列的数量,然后用key/colLen,key%colLen来得到i,j下标效率更高。

Talk is cheap>>

解法一:

  public void solve(char[][] board) {
Queue<Integer> queue = new ArrayDeque<>();
int rowLen = board.length;
if (rowLen == 0)
return;
int colLen = board[0].length;
int[][] adj = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
          //如果O在四条边上
if (board[i][j] == 'O' && (i == 0 || j == 0 || i == rowLen - 1 || j == colLen - 1)) {
int key = i * colLen + j;
board[i][j] = '1';
queue.add(key);
while (!queue.isEmpty()) {
              //BFS遍历与边上的O连通的O
key = queue.poll();
int x = key / colLen;
int y = key % colLen;
for (int k = 0; k < 4; k++) {
int adj_x = x + adj[k][0];
int adj_y = y + adj[k][1];
if (adj_x >= 0 && adj_y >= 0 && adj_x < rowLen && adj_y < colLen) {
if (board[adj_x][adj_y] == 'O') {
int pos = adj_x * colLen + adj_y;
board[adj_x][adj_y]='1';
queue.add(pos);
}
}
}
}
}
}
}
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == '1') {
board[i][j] = 'O';
}
}
}
}

解法二:

  public void solve(char[][] board) {
Queue<Integer> queue = new ArrayDeque<>();
int rowLen = board.length;
if (rowLen == 0) {
return;
}
int[][] adj = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int colLen = board[0].length;
boolean[][] visited = new boolean[rowLen][colLen];
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
if (!visited[i][j] && board[i][j] == 'O') {
            //标准的BFS遍历
boolean surround = true;
List<Integer> toHandle = new ArrayList<>();
queue.add(i * colLen + j);
while (!queue.isEmpty()) {
int key = queue.poll();
toHandle.add(key);
int x = key / colLen;
int y = key % colLen;
for (int k = 0; k < 4; k++) {
                //检查O的上下左右
int adj_x = x + adj[k][0];
int adj_y = y + adj[k][1];
if (adj_x >= 0 && adj_y >= 0 && adj_x < rowLen && adj_y < colLen) {
                  //都不在边上
if (board[adj_x][adj_y] == 'O' && !visited[adj_x][adj_y]) {
int pos = adj_x * colLen + adj_y;
queue.add(pos);
visited[adj_x][adj_y] = true;
}
} else {
                  //有一个在边上,置surround=false
surround = false;
}
}
}
if (surround) {
for (int key : toHandle) {
board[key / colLen][key % colLen] = 'X';
}
}
}
}
}
}

Surrounded Regions——LeetCode的更多相关文章

  1. Surrounded Regions leetcode java

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

  2. Surrounded Regions - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 https://leetcode.com/problems/surrounded-regions/ 注意点 边缘不算包围'O' 解法 解法一:dfs.找处 ...

  3. [LeetCode] Surrounded Regions 包围区域

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

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

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

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

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

  6. Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)

    Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...

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

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

  8. 【leetcode】Surrounded Regions

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

  9. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

随机推荐

  1. PHP安全编程:主机文件目录浏览(转)

    除了能在共享服务器上读取任意文件之外,攻击者还能建立一个可以浏览文件系统的脚本.由于你的大多数敏感文件不会保存在网站主目录下,此类脚本一般用于找到你的源文件的所在位置.请看下例: 01 <?ph ...

  2. 多线程(NSThread、NSOperation、GCD)编程浅谈

    一.基本概念 进程:一个具有一定独立功能的程序关于某个数据集合的一次运行活动.可以理解成一个运行中的应用程序.线程:程序执行流的最小单元,线程是进程中的一个实体.同步:只能在当前线程按先后顺序依次执行 ...

  3. js select 实现左右传值.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Retrofit2 简介 语法 案例

    简介 官网:http://square.github.io/retrofit/ GitHub:https://github.com/square/retrofit/ compile 'com.squa ...

  5. Sublime Text3使用详解

    Sublime Text简介 Sublime Text - 性感的代码编辑器.程序员之必备神器 Sublime Text 是一个代码编辑器,也是HTML和散文先进的文本编辑器.Sublime Text ...

  6. MySQL+PHP配置 Windows系统IIS版(转)

    1.下载 MySQL下载地址:http://dev.mysql.com/downloads/mysql/5.1.html->Windows (x86, 32-bit), MSI Installe ...

  7. 网页嵌入百度地图和使用百度地图api自定义地图的详细步骤

    在网页中插入百度地图 如果想在自己的网页上面加入百度地图的话,可以用百度地图的api.具体使用方法如下: 第一步:进入百度创建地图的网站http://api.map.baidu.com/lbsapi/ ...

  8. redisbook笔记——redis内存映射数据结构

    虽然内部数据结构非常强大,但是创建一系列完整的数据结构本身也是一件相当耗费内存的工作,当一个对象包含的元素数量并不多,或者元素本身的体积并不大时,使用代价高昂的内部数据结构并不是最好的办法. 为了解决 ...

  9. 使用下拉列表框<select>标签,节省空间

    下拉列表在网页中也常会用到,它可以有效的节省网页空间.既可以单选.又可以多选.如下代码: 讲解: 1.value: 2.selected="selected": 设置selecte ...

  10. Swift - 15 - 导入Foundation使用更多字符串功能

    //: Playground - noun: a place where people can play import Foundation var str = "Hello, playgr ...