LeetCode: Surrounded Regions 解题报告
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 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

SOLUTION 1:
经典 BFS 题目。如果使用DFS,会超时。
使用队列进行BFS。注意,在判断index越界时,主页君写了2种方法。
(1)可以直接对index在0-x*y之间取。这里有个小的trick,在点在左边缘的时候,index-1会导致我们回到上一行的最右边。当然那个点也是
边缘点,所以不会造成解的错误。
(2)判断点是不是在边缘,判定上下左右还有没有节点。
常出错的点:计算index是i * cols + j 这点要记好了。
public class Solution {
public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) {
return;
}
int rows = board.length;
int cols = board[0].length;
// the first line and the last line.
for (int j = 0; j < cols; j++) {
bfs(board, 0, j);
bfs(board, rows - 1, j);
}
// the left and right column
for (int i = 0; i < rows; i++) {
bfs(board, i, 0);
bfs(board, i, cols - 1);
}
// capture all the nodes.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == 'B') {
board[i][j] = 'O';
}
}
}
return;
}
public void bfs1(char[][] board, int i, int j) {
int rows = board.length;
int cols = board[0].length;
Queue<Integer> q = new LinkedList<Integer>();
q.offer(i * cols + j);
while (!q.isEmpty()) {
int index = q.poll();
// Index is out of bound.
if (index < 0 || index >= rows * cols) {
continue;
}
int x = index / cols;
int y = index % cols;
if (board[x][y] != 'O') {
continue;
}
board[x][y] = 'B';
q.offer(index + 1);
q.offer(index - 1);
q.offer(index + cols);
q.offer(index - cols);
}
}
public void bfs(char[][] board, int i, int j) {
int rows = board.length;
int cols = board[0].length;
Queue<Integer> q = new LinkedList<Integer>();
q.offer(i * cols + j);
while (!q.isEmpty()) {
int index = q.poll();
int x = index / cols;
int y = index % cols;
if (board[x][y] != 'O') {
continue;
}
board[x][y] = 'B';
if (y < cols - 1) {
q.offer(index + 1);
}
if (y > 0) {
q.offer(index - 1);
}
if (x > 0) {
q.offer(index - cols);
}
if (x < rows - 1) {
q.offer(index + cols);
}
}
}
}
SOLUTION 2:
附上DFS解法:
public void dfs(char[][] board, int i, int j) {
int rows = board.length;
int cols = board[0].length;
// out of bound or visited.
if (i < 0 || i >= rows || j < 0 || j >= cols) {
return;
}
if (board[i][j] != 'O') {
return;
}
board[i][j] = 'B';
// dfs the sorrounded regions.
dfs(board, i + 1, j);
dfs(board, i - 1, j);
dfs(board, i, j + 1);
dfs(board, i, j - 1);
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/bfs/Solve.java
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: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [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 ...
随机推荐
- JAVA Drp项目实战—— Unable to compile class for JSP 一波三折
交代下背景.电脑系统是64位的,用的是64位的Tomcat.安装是32位的Myeclipse10,java环境也是32位的.Tomcat在開始启动时会报这样一个错误,"Can't load ...
- 对opencv MeanShift 融合矩形框的改进
OPENCV 中的代码改进.当然要依据自己的实际情况来,OPENCV 中行人检測有两种矩形框的融合算法.这里仅仅对meanshift 方法做改进 假设有更好的方法.希望能够跟我讲下. 对于去除重合部分 ...
- Eclipse集成ijkplayer并实现本地和网络视频播放等
概述 Eclipse 集成ijkplayer demo,播放本地视频.和rtmp流. 详细 代码下载:http://www.demodashi.com/demo/10630.html 原文地址:Ecl ...
- 社区类 App 如何引导用户发帖和产生内容?
作者:Pmer在路上链接:http://www.zhihu.com/question/25502904/answer/31342246来源:知乎著作权归作者所有,转载请联系作者获得授权. ugc的产出 ...
- 打开Activity时,不自动显示(弹出)虚拟键盘
打开Activity时,不自动显示(弹出)虚拟键盘 在AndroidManifest.xml文件中<activity>标签中添加属性 android:windowSoftInputMode ...
- IDEA删除项目
IDEA没有eclipse的右键直接在磁盘delete整个项目的功能,使用IDEA删除项目需要按照如下步骤: step1:右击项目——>Remove Module 之后会出现提示框如下: 意思是 ...
- 深入PHP内核之函数和返回值
1.关于返回值,PHP内核中使用了大量的宏来实现,我们先看一个函数 PHP_FUNCTION 宏的定义(Zend/zend_API.h) #define PHP_FUNCTION ZEND_FUNC ...
- 使用Dockerfile文件构建基于centOS系统的tomcat镜像
以下是Dockerfile的内容: #基础镜像 FROM centos #维护人员信息 MAINTAINER weigs "weigs1231@gmail.com" #设置工作目录 ...
- Android四款系统架构工具
开发者若想开发出一款高质量的应用,一款功能强大的开发工具想必是不可或缺的.开发工具简化了应用的开发流程,也能使开发者在应用开发本身投入更多的精力.本文就为大家带来4款实用的Android应用架构工具. ...
- jsp地址栏传中文显示乱码解决方法
格式一: 地址栏显示格式:http://localhost:8081/Jsp2/ahref2.jsp?id=32&name=%E7%8E%8B%E4%BA%91%E9%B9%8F 1.修改To ...