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 动态生成类再编译最后代理
package spring.vhostall.com.proxy; public interface Store { public void sell(); } ------------------ ...
- CF MVC3此操作要求连接到 'master' 数据库。无法创建与 'master' 数据库之间的连接,这是因为已打开原始数据库连接,并且已从连接字符串中删除凭据。请提供未打开的连接 解决方法
<add name="ProwebEntities" connectionString ="Data Source=.;Integrated Security=tr ...
- Flash:DisplayObject的矩阵旋转(移动/修改注册点,修改旋转点)
简单来说,原理就是利用matrix运算,先把旋转点移到原点位置,旋转变换后再恢复到原来的位置 var a:Sprite = new Sprite(); a.graphics.beginFill(0); ...
- struts2接收参数的几种形式
1.使用属性 HTML: <form action="login" method="post" name="form1"> 用户 ...
- 如何高效的阅读hadoop源代码?
个人谈谈阅读hadoop源代码的经验. 首先,不得不说,hadoop发展到现在这个阶段,代码已经变得非常庞大臃肿,如果你直接阅读最新版本的源代码,难度比较大,需要足够的耐心和时间,所以,如果你觉得认真 ...
- virtualenv沙箱
准备用Django开发网站,豆瓣阅读中有<Django入门>,里面有人批注说用virtualenv+pip很好. 每次搞个什么都感觉举步维艰,因为之前太懒了,什么都懒得深入看一下.pip一 ...
- C实现9种排序算法
算法复杂度以及稳定性分析 算法名称 平均时间 辅助空间 稳定性 冒泡排序 O(n2) O(1) 是 选择排序 O(n2) O(1) 否 插入排序 O(n2) O(1) 是 自底向上归并排序 O(nlo ...
- ArchLinux下LXDE的安装与设置心得
安装 首先安装基本的桌面环境: $ sudo pacman -S lxde 奇怪的是默认并没有安装面板lxpanel,而且源里也没有,只好从AUR安装之: $ yaourt -S lxpanel-sv ...
- 使用CXF实现基于Rest方式的WebService(转)
转自:https://www.cnblogs.com/zjm701/p/6845813.html原文更清晰 本文介绍使用CXF实现基于Rest方式的WebService(CXF的版本是3.0.0) 一 ...
- 【LeetCode】120. Triangle (3 solutions)
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...