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 ...
随机推荐
- C# 的Timer 在javascript中的实现--基于Typescript
class Timer { //js 内置的timer对象 private _jsInnerTimerObj: any; private _enable: boolean; private _hand ...
- 如何在 Linux 下调试动态链接库
大家都知道在 Linux 可以用 gdb 来调试应用程序,当然前提是用 gcc 编译程序时要加上 -g 参数.我这篇文章里将讨论一下用 gdb 来调试动态链接库的问题. 首先,假设我们准备这样的一个动 ...
- 重叠IO overlapped I/O 运用详解
2009年02月21日 星期六 下午 07:54 I/O设备处理必然让主程序停下来干等I/O的完成,对这个问题有 方法一:使用另一个线程进行I/O.这个方案可行,但是麻烦. ...
- IBM InfoSphere DataStage 8.1 DataStage Job 开发具体解释
简单介绍 DataStage 使用了 Client-Server 架构,server端存储全部的项目和元数据,client DataStage Designer 为整个 ETL 过程提供了一个图形化的 ...
- 将Memcached作为服务自动启动
1.最简单的做法 通常:启动Memcache的服务器端的命令为: /usr/local/bin/memcached -d -m 256 -u root -l 127.0.0.1 -p 12000 -c ...
- iOS开发-Objective-C Block的实现方式
前言:我们可以把Block当作一个闭包函数,它可以访问外部变量和局部变量,但默认是不可以修改外部变量.你可以使用它来做回调方法,比起使用代理(Delegate)会更加直观.顺带一提,苹果很多的接口(A ...
- Archlive新年第一棒: 基于2.6.37稳定内核的archlive20110107
先上图,再来说明吧... 下载地址: http://u.115.com/file/t2cd0ea120 先上个本机器运行teamviewer的效果图吧... 如假包换的 2.6.37, 担保是目前最 ...
- MyEclipse 8.6插件下载
(源自网络:http://hi.baidu.com/%D4%B5%BA%A3%C7%E9%C9%EE/blog/item/ad86323d1e80a5e33d6d97c6.html 和 http:/ ...
- iOS archive(归档)
归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存(实际上是一种文件保存的形式),浏览网上的一些资料后,并结合自己的一些经验,总结成此文. 一.使用archiveRootObject进 ...
- Android开发学习之Activity的简介
1.Activity的概念介绍 Activity是Android组件中最基本也是最常用的一种组件,在一个Android应用中,一个Activity通常就是一个单独的屏幕.每一个Activity都被实现 ...