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 ...
随机推荐
- windows installer服务无法启动,无法打开任何msi文件
如果不成功就在"依存关系"中找是否有其他的文件没有启用. 启用"remote procedure call(rpc)" 启用"workstation& ...
- idea 配置多个jdk
1.File -->Project Structure 2.SDKs-->点击+号-->JDK-->目录选择到jdk文件夹所在的位置 3.选择jdk所在路径 4.可以配置多 ...
- maven中央仓库
https://search.maven.org (查看版本和文件列表:http://repo1.maven.org/maven2/) http://mvnrepository.com/ https: ...
- urlparse模块(专门用来解析URL格式)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #urlparse模块(专门用来解析URL格式) #URL格式: #protocol ://hostname[ ...
- spring aop的两种写法aspect和advisor
本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...
- 史上最全的iOS面试题及答案,且看且珍藏,错过就没有喽!
1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答:Object-c的类不可以多重继承;可以实现多个接口,通过实现多 ...
- Linux命令-防火墙命令:iptables
iptables详解--转 查看防火墙设置: 第一种方式: cat /etc/sysconfig/iptables 第二种方式: iptables -L -n -v 设置防火墙: 第一种方式: ipt ...
- Android Scrollview嵌套RecyclerView导致滑动卡顿问题解决
一个比较长的界面一般都是Scrollview嵌套RecyclerView来解决.不过这样的UI并不是我们开发人员想看到的,实际上嵌套之后.因为Scrollview和RecyclerView都是滑动控件 ...
- Python asin() 函数
描述 asin() 返回x的反正弦弧度值. 语法 以下是 asin() 方法的语法: import math math.asin(x) 注意:asin()是不能直接访问的,需要导入 math 模块,然 ...
- EMC测试
EMC主要包括EMI和EMS