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 ...
随机推荐
- SettingsJDK
迁移时间:2017年5月20日23:38:40 Author:Marydon 1.双击安装,更改安装路径为D:\ProgramFiles\Java\jdk1.7.0_55: 注意事项: 1.1 将 ...
- Linux应用小技巧
简介 本文针对Linux操作过程中提升工作效率问题,给出常见操作技巧,主要从Linux终端管理.显示git分支.终端快速检索历史命令等方面进行介绍. 本文内容主要以Ubuntu系统为例进行介绍. Li ...
- 腾讯云-NGINX搭建静态网站
搭建静态网站 搭建Http静态服务器环境 任务时间:15min ~ 30min 搭建静态网站,首先需要部署环境.下面的步骤,将告诉大家如何在服务器上通过 Nginx 部署 HTTP 静态服务. 00. ...
- reindex-maven 私服(nexus)架设以及项目管理中遇到的问题及解决方案(updating)
--- 用maven 的过程中 大问题小问题实在是不少 ,就不一篇文章一篇文章的写了,干脆写在一起 ---- ------- nexus 加索引 点击Administration菜单下面的Re ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- H5版如何在微信外(非微信浏览器)进行微信支付技术方案
官方是支持在非微信内置浏览器中调起微信支付的!H5支付是基于公众号基础开发的一种非微信内浏览器支付方式(需要单独申请支付权限),可以满足在微信外的手机H5页面进行微信支付的需求.同时,由于H5链接传播 ...
- RHEL6.4 多路径绑定
# rpm -qa | grep mapper #查看multipath是否安装 # lsmod | grep dm_multipath #查看multipath模块是否加载 # rpm -ivh | ...
- PLSQL_通过UTL_MAIL发送并发程式结果报表至用户邮箱(案例)
2014-06-01 Created By BaoXinjian
- json字符串序列化exception处理
一.背景: 使用REST接口接收远端传送过来的Json格式String,需要把这个String序列化成响应的对象. 二.问题: 对方封装了一个错误的json格式过来,程序就挂了…… 三.似乎解决: 通 ...
- Jquery 对比 Javascript
转自 http://www.webhek.com/you-do-not-need-jquery AJAX JSON JQUERY $.getJSON('/my/url', function(data) ...