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 ...
随机推荐
- XMPP serverejabberd-14.12本地搭建
这里记录下我搭建ejabberd-14.12的过程. 首先下载:http://pan.baidu.com/s/1hqzjezq 这里我传到我的网盘一份, 大家也能够到官网下载http://xmpp. ...
- 如何监控tomcat性能
如何监控tomcat性能:[1]工具一 | 浏览:155 | 更新:2014-12-13 10:06 1 2 3 4 5 6 分步阅读 tomcat经常被用作中间件,也有直接作WEB的,自带的工具不是 ...
- MySQL Workbench--Window安装试用
MySQL Workbench 01.概述 MySQL Workbench provides DBAs and developers an integrated tools environment f ...
- 自定义类似于listView中Item背景
方法一. drawable/listitem_bk.xml <?xml version="1.0" encoding="utf-8" ?> < ...
- python学习笔记——多进程一 基础概念
1 进程 进程:程序的一次(从开始到结束)执行过程,属于一个动态过程.是系统进行资源分配和调度的基本单位. 程序:指的是一个文件,磁盘中可执行的代码.属于一个静态文件 注:进程运行时需要把程序加载如内 ...
- Ubuntu Pycharm不能同时选中多行解决方法
转自http://blog.csdn.net/yaoqi_isee/article/details/77866309 问题描述 Pycharm和Sublime有一个很好用的特性就是可以同时选中多行进行 ...
- oc引入头文件
建立.h的头文件(header file)命名为Hi.h,源码如下: #ifndef oc3_Hi_h #define oc3_Hi_h void sayHi(); #endif 建立.c的头文件(c ...
- 如何不让DataGridView自动生成列
如果不想让DataGridView自动生成与数据源对应的列, 只需要把属性AutoGenerateColumns设为false即可. 需要注意: 在界面设计的属性窗口中是看不到AutoGenerate ...
- Service和广播联合更新UI的例子
sa111111 于 2010-11-19 10:56 发表在 [Android实例] [复制链接] [只看楼主] [上一主题] [下一主题] 在Android中,异步更新UI,通常我们会选用Ha ...
- Linux 批量建立信任关系,实现ssh无password登陆的脚本
作用: 把当前机器的ssh公钥拷贝到其它机器.以实现从当前机器能够ssh无password登陆到其它机器 用法: 1)把要加入的ip地址写入到一个文件里.比方host 2)把脚本保存为sh文件,如 i ...