题目:

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

链接:   http://leetcode.com/problems/surrounded-regions/

题解:

第一思路是从四条边的'O'开始做BFS,类似查找Connected Components或者图像处理里面的Region Grow。把找到的'O'标记为一个特殊符号比如'1'之类的。BFS结束后scan整个矩阵,把剩下的'O'变成'X','1'变回'O'。

Time Complexity - O(mn), Space Complexity - O(mn)

public class Solution {
public void solve(char[][] board) {
if(board == null || board.length == 0)
return;
Queue<Node> q = new LinkedList<>(); for(int i = 0; i < board.length; i++) // first col
if(board[i][0] == 'O')
q.offer(new Node(i, 0)); for(int i = 0; i < board.length; i++) // last col
if(board[i][board[0].length - 1] == 'O')
q.offer(new Node(i, board[0].length - 1)); for(int i = 1; i < board[0].length - 1; i++) // first row
if(board[0][i] == 'O')
q.offer(new Node(0, i)); for(int i = 1; i < board[0].length - 1; i++) // last row
if(board[board.length - 1][i] == 'O')
q.offer(new Node(board.length - 1, i)); while(!q.isEmpty()) {
Node curNode = q.poll();
if(curNode.row < 0 || curNode.row >= board.length || curNode.col >= board[0].length || curNode.col < 0)
continue;
if(board[curNode.row][curNode.col] != 'O')
continue;
board[curNode.row][curNode.col] = '|';
q.offer(new Node(curNode.row - 1, curNode.col));
q.offer(new Node(curNode.row + 1, curNode.col));
q.offer(new Node(curNode.row, curNode.col + 1));
q.offer(new Node(curNode.row, curNode.col - 1));
} for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++)
if(board[i][j] == 'O')
board[i][j] = 'X'; for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++)
if(board[i][j] == '|')
board[i][j] = 'O';
} private class Node {
public int row;
public int col;
public Node(int i, int j) {
this.row = i;
this.col = j;
}
}
}

代码有些长,有空一定要看一看discuss版简洁的解法,再来更新。 (看了一下好像大家写得也都比较长...算了先不更新了,Reference里的文章跟我的解法一样,不过说明更详细准备,挺好的)。

题外话: 今天Work from home去洗牙,洗完牙以后医生说你最里面的大牙蛀了,裂了一条缝,需要补。长这么大第一次蛀牙,前些天总觉得不舒服,勤刷牙也无济于事。赶紧跟医生约好下周去补,拖延的话也许就要根管了。之后去Costco买了葡萄苹果和香蕉,家里还有三个桃子,可以靠吃水果度日。

中午两点的时候兴业银行电面,法国人问了一些基本的OO问题,还有SQL问题,20多分钟就结束了,感觉双方兴趣都不大...对这些银行,我要奉行不主动,不拒绝,他们有兴趣我就面,就当熟悉简历和练口语了。

二刷:

重新写了一下,要先建立一个Node来保存坐标。然后使用一个Queue来对Node进行BFS。下面的写法里面有不少多余的计算,需要进一步好好剪枝。

Java:

Time Complexity - O(4mn), Space Complexity - O(4mn)

public class Solution {
public void solve(char[][] board) { // BFS
if (board == null || board.length == 0) return;
Queue<Node> q = new LinkedList<>();
int rowNum = board.length;
int colNum = board[0].length;
for (int i = 0; i < board.length; i++) {
if (board[i][0] == 'O') q.offer(new Node(i, 0));
if (board[i][colNum - 1] == 'O') q.offer(new Node(i, colNum - 1));
}
for (int j = 0; j < board[0].length; j++) {
if (board[0][j] == 'O') q.offer(new Node(0, j));
if (board[rowNum - 1][j] == 'O') q.offer(new Node(rowNum - 1, j));
} while (!q.isEmpty()) {
Node node = q.poll();
int i = node.row, j = node.col;
if (board[i][j] != 'O') continue;
board[i][j] = '|';
if (i - 1 >= 0 && board[i - 1][j] == 'O') q.offer(new Node(i - 1, j));
if (i + 1 <= rowNum - 1 && board[i + 1][j] == 'O') q.offer(new Node(i + 1, j));
if (j - 1 >= 0 && board[i][j - 1] == 'O') q.offer(new Node(i, j - 1));
if (j + 1 <= colNum - 1 && board[i][j + 1] == 'O') q.offer(new Node(i, j + 1));
} for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
if (board[i][j] == 'O') board[i][j] = 'X';
}
}
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
if (board[i][j] == '|') board[i][j] = 'O';
}
}
} private class Node {
int row;
int col; public Node(int i, int j) {
this.row = i;
this.col = j;
}
}
}

三刷:

依然使用了BFS,简化了一下。速度仍然不是很快,大约9ms左右。需要进一步优化,或者尝试Union-Find和DFS。

Java:

Time Complexity - O(mn), Space Complexity - O(mn)

public class Solution {
public void solve(char[][] board) { // BFS
if (board == null || board.length == 0) return;
Queue<int[]> q = new LinkedList<>();
int rowNum = board.length;
int colNum = board[0].length;
for (int i = 0; i < rowNum; i++) {
if (board[i][0] == 'O') q.offer(new int[] {i, 0});
if (board[i][colNum - 1] == 'O') q.offer(new int[] {i, colNum - 1});
}
for (int j = 1; j < colNum - 1; j++) {
if (board[0][j] == 'O') q.offer(new int[] {0, j});
if (board[rowNum - 1][j] == 'O') q.offer(new int[] {rowNum - 1, j});
} while (!q.isEmpty()) {
int[] node = q.poll();
int i = node[0], j = node[1];
if (board[i][j] != 'O') continue;
board[i][j] = '|';
if (i - 1 >= 0 && board[i - 1][j] == 'O') q.offer(new int[] {i - 1, j});
if (i + 1 <= rowNum - 1 && board[i + 1][j] == 'O') q.offer(new int[] {i + 1, j});
if (j - 1 >= 0 && board[i][j - 1] == 'O') q.offer(new int[] {i, j - 1});
if (j + 1 <= colNum - 1 && board[i][j + 1] == 'O') q.offer(new int[] {i, j + 1});
} for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
if (board[i][j] == 'O') board[i][j] = 'X';
}
}
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
if (board[i][j] == '|') board[i][j] = 'O';
}
}
}
}

Reference:

https://leetcode.com/discuss/19805/my-java-o-n-2-accepted-solution

https://leetcode.com/discuss/97838/share-my-4ms-improved-solution-beats-99-5%25

https://leetcode.com/discuss/42445/a-really-simple-and-readable-c-solution%EF%BC%8Conly-cost-12ms

https://leetcode.com/discuss/6285/solve-it-using-union-find

https://leetcode.com/discuss/45746/9-lines-python-148-ms

130. Surrounded Regions的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

  3. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  4. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  5. Leetcode 130. Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  6. 130. Surrounded Regions -- 被某字符包围的区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  7. 【一天一道LeetCode】#130. Surrounded Regions

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 130. Surrounded Regions (Graph; DFS)

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  9. 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

随机推荐

  1. c#基础学习汇总----------base和this,new和virtual

    base和this是c#中的两访问关键字,目的是用于实现继承机制的访问操作,来满足对对象成员的访问,从而为多态机制提供更加灵活的处理方式. 在看<你必须知道的.Net>一书中有一个例子很好 ...

  2. 0<=i<iLen 在C++中

    for( i=0;0<= i<2; i++)这样的话会出现什么错误呢? 一直循环下去, 因为i>=一直成立

  3. 对C#调用C++ dll文件进行总结

    在实际项目工作中,经常用到C#调用C++ 或者C编写的dll文件. dll支持一般函数声明和类的定义声明,但是一般为了简化,都是 采用函数声明的方式.这里主要并不是写 dll的编写. 先在vs中创建一 ...

  4. [java学习笔记]java语言基础概述之函数的定义和使用&函数传值问题

    1.函数 1.什么是函数? 定义在类中的具有特定功能的一段独立小程序. 函数也叫做方法 2.函数的格式 修饰符   返回值类型    函数名(参数类型  形式参数1, 参数类型  形式参数2-) { ...

  5. 个人站长如何使用svn发布到服务器不遗漏文件

    作为个人站长,最最头疼的一件事情就是在本地开发好代码之后,上传的时候要去服务器上一个一个文件进行覆盖,添加操作:是人难免出错,避免这种情况的方法: 开发者最好是在本地有一个代码库,创建好代码库之后,至 ...

  6. Bind安装配置及应用

    Bind安装配置及应用 BIND:Berkeley Internet Name Domain ,ISC.org     DNS服务的实现:     监听端口:53/UDP , 53/TCP     程 ...

  7. DTCMS自定义标签:面包屑导航,栏目中通过栏目调用名称获得栏目名称

    DTcms.Web.UI\Label\category.cs中增加标签 /// <summary> /// 自定义:通过类别name获得类别title /// </summary&g ...

  8. 借网上盛传2000w记录介绍多进程处理

    2000w的数据在网上搞得沸沸扬扬,作为技术宅的我们也来凑凑热闹.据了解网上有两个版一个是数据库文件另一个是CSV文件的,前者大小有好几个G后者才几百M.对于不是土豪的我们当然下载几百M的.至于在哪下 ...

  9. asp.net webform javascript postback JSON

    在弹出界面点击按钮触发后台的postback而不是刷新界面(保留已存在的搜索条件) function filterData() { var data = { col: $('#filterPopup' ...

  10. RequireJS入门与进阶

    RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...