Surrounded Regions——LeetCode
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的更多相关文章
- Surrounded Regions leetcode java
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- Surrounded Regions - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 https://leetcode.com/problems/surrounded-regions/ 注意点 边缘不算包围'O' 解法 解法一:dfs.找处 ...
- [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】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)
Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 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 用了第一种方式, ...
随机推荐
- JAVA IDE基本操作常识
快捷键: Ctrl+/ 选中区单行注释和 取消 选中区单行注释和 Alt + / 代码辅助 shift + Ctrl +/ 选中区多行注释 shift + Ctrl +\ 取消选中区多行注释 Ct ...
- 自定义filter包
在有些时候,你可能需要以你的所有项目进行全局的过滤. 因为你的项目可以设计到互相的依赖和调用 . 修改在tomcat下的conf下的web.xml文件.和在原来的web-inif下的修改一样,添加fi ...
- Android开发--WIFI实现
wifi的基本结构 在Android的官方文档中定义了如下五种状态: WIFI_STATE_DISABLING WIFI网卡正在关闭 0 WIFI_STATE_DISABLED WIFI网卡不 ...
- C#多线程lock解决数据同步
1.代码实例: public class ThreadTest4 { public static void Init() { //多个线程修改同一个值,使用lock锁解决并发 ; i < ; i ...
- 织梦dede自定义内容分页,datalist运用实例
在/plus文件夹中新建一个ceshi.php文件..<?php require(dirname(__FILE__)."/../include/common.inc.php" ...
- 困扰:C#.net 连接Oracle11g 不报错但是在connection时出现 ServerVersion 引发了“System.InvalidOperationException”类型的异常
今天在使用VS2008 32位 连接 64位的Oracle11g的数据库时出现 “conn.ServerVersion”引发了“System.InvalidOperationException”类型的 ...
- hibernate_validator_07
一.校验组序列 默认情况下,约束的验证是没有一定的顺序的,不管他们是属于哪个认证组的.但是在有些环境中,我们控制这些约束验证的顺序还是很有用的. 就拿我们上一个例子来说,我们可以这样:首先在我们检查车 ...
- 二叉树中序遍历 (C语言实现)
在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构.二叉树是每个节点最多有两个子树的有序树.通常子树被称作“左子树”(left subtre ...
- openstack nova数据库计算结点IP地址
最近遇到一个问题就是在控制结点上查找nova数据库中 select * from compute_nodes\G;中出现IP地址一直是127.0.0.1不是计算结点的IP,就算修改成计算结点的IP,也 ...
- Build Android-x86 ICS 4 Virtualbox from Google Virtualbox Target and Intel Kernel 编译体验
最近一直在研究android源码的编译,应该说研究的很辛苦,最难的是下源码,总是不停的断掉,最后感谢公司的高网速,找到方法后12G的源码只花了1个小时就下完了. 参考以下网址:http://softw ...