Given a 2D board containing 'X' and 'O' (the letter 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

像围棋一样,去掉被包围的O,刚开始用了HashSet<Integer>但是内存溢出了。

public class Solution {
public void solve(char[][] board) {
int row = board.length;
if( row < 2 )
return ;
int col = board[0].length;
if( col < 2 )
return ; for( int i = 0;i<row;i++){
for( int j = 0;j<col;j++){
if( board[i][j] == 'O'){
HashSet<Integer> set = new HashSet<Integer>();
if( helper(board,i,j,set,false) ){
helpset('o',set,board);
}else
helpset('X',set,board);
}
}
}
for( int i = 0;i<row;i++){
for( int j = 0;j< col;j++){
if( board[i][j] == 'o')
board[i][j] = 'O';
}
}
} public void helpset(char ch ,HashSet<Integer> set ,char[][] board){ for( int i : set ){
int row = i/board[0].length;
int col = i%board[0].length;
board[row][col] = ch;
}
} public boolean helper(char[][] board,int num1, int num2,HashSet<Integer> set,boolean flag){
boolean result = flag;
if( num1-1 >= 0 && board[num1-1][num2] == 'O'){
int num = (num1-1)*board[0].length+num2;
if( !set.contains(num) ){
set.add(num);
result = result || helper(board,num1-1,num2,set,flag);
}
}
if( num1+1 < board.length && board[num1+1][num2] == 'O'){
int num = (num1+1)*board[0].length+num2;
if( !set.contains(num) ){
set.add(num);
result = result || helper(board,num1+1,num2,set,flag);
}
}
if( num2-1 >= 0 && board[num1][num2-1] == 'O'){
int num = num1*board[0].length+num2-1;
if( !set.contains(num) ){
set.add(num);
result = result || helper(board,num1,num2-1,set,flag);
}
}
if( num2+1 < board[0].length && board[num1][num2+1] == 'O'){
int num = num1*board[0].length+num2+1;
if( !set.contains(num) ){
set.add(num);
result = result || helper(board,num1,num2+1,set,flag);
}
} if( num1 == 0 || num1 == board.length-1 || num2 == 0 || num2 == board[0].length-1)
return true; return result; }
}

2、换用思路,先把所有的O放入一个set中,然后在这个set中进行判断,虽然ac但是还是耗时还是较长。

public class Solution {
public void solve(char[][] board) { int row = board.length;
if( row < 2 )
return ;
int col = board[0].length;
if( col < 2 )
return ;
HashSet<Integer> set = new HashSet<Integer>();
for( int i = 0;i<row;i++){
for( int j = 0;j<col;j++){
if( board[i][j] == 'O')
set.add(i*board[0].length+j);
}
}
while( !set.isEmpty() ){
helper(set,col,row,board);
}
return ;
}
public void helper(HashSet<Integer> set,int len,int row,char[][] board){ boolean result = false;
Queue<Integer> queue = new LinkedList<Integer>();
int[] set2 = new int[set.size()];
int num = set.iterator().next();
set2[0] = num;
int i = 1;
queue.add(num);
set.remove(num);
if (num % len == 0 || num % len == len - 1 || num / len == 0 || num / len == row - 1)
result = true;
while( !queue.isEmpty() ){
num = (Integer) queue.poll();
if( set.contains(num+1) ){
set2[i] = num+1;
i++;
queue.add(num+1);
set.remove(num+1);
if ((num+1) % len == 0 || (num+1) % len == len - 1 || (num+1) / len == 0 || (num+1) / len == row - 1)
result = true;
}
if( set.contains(num-1)){
set2[i] = (num-1);
i++;
queue.add(num-1);
set.remove(num-1);
if ((num-1) % len == 0 ||( num-1) % len == len - 1 || (num-1) / len == 0 || (num-1) / len == row - 1)
result = true;
}
if( set.contains(num+len) ){
set2[i] = (num+len);
i++;
queue.add(num+len);
set.remove(num+len);
if ((num+len) % len == 0 || (num+len )% len == len - 1 || (num+len) / len == 0 || (num+len) / len == row - 1)
result = true;
}
if( set.contains(num-len) ){
set2[i] = (num-len);
i++;
queue.add(num-len);
set.remove(num-len);
if ((num-len) % len == 0 || (num-len) % len == len - 1 || (num-len) / len == 0 || (num-len) / len == row - 1)
result = true;
}
}
if( result == true )
return ;
else
helpset('X',set2,i,board);
}
public void helpset(char ch ,int[] set,int num ,char[][] board){
for( int i = 0;i<num;i++){
int row = set[i]/board[0].length;
int col = set[i]%board[0].length;
board[row][col] = ch;
}
}
}

3、使用BFS和队列。

public class Solution {
public void solve(char[][] board) {
int row = board.length;
if( row < 2 )
return ;
int col = board[0].length;
if( col < 2 )
return ;
Queue<Integer> queue = new LinkedList<Integer>();
for( int i = 0;i<col;i++){
if( board[0][i] == 'O' )
queue.add(i);
if( board[row-1][i] == 'O')
queue.add((row-1)*col+i);
}
for( int i = 0;i < row ;i++){
if( board[i][col-1] == 'O')
queue.add(i*col+col-1);
if( board[i][0] == 'O')
queue.add(i*col);
}
while( !queue.isEmpty() ){
int num = queue.poll();
int x = num/col,y = num%col;
if( board[x][y] != 'O')
continue;
board[x][y] = 'o';
if( x-1>=0 && board[x-1][y] == 'O')
queue.add(num-col);
if( x+1<row && board[x+1][y] == 'O')
queue.add(num+col);
if( y-1>=0 && board[x][y-1] == 'O')
queue.add(num-1);
if( y+1<col && board[x][y+1] == 'O')
queue.add(num+1);
} for( int i = 0;i<row;i++){
for( int j = 0;j<col;j++){
if( board[i][j] == 'O')
board[i][j] = 'X';
else if( board[i][j] == 'o')
board[i][j] = 'O';
}
} return ;
}
}

4、不使用队列,直接用BFS。

public class Solution {
public void solve(char[][] board) {
if (board.length == 0) return;
int row = board.length;
int col = board[0].length; for (int i = 0; i < row; i++) {
if (board[i][0] == 'O')
dfs(board, i, 0);
if (board[i][col-1] == 'O')
dfs(board, i, col-1);
} for (int i = 1; i < col - 1; i++) {
if (board[0][i] == 'O')
dfs(board, 0, i);
if (board[row - 1][i] == 'O')
dfs(board,row-1, i);
} for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == '1') {
board[i][j] = 'O';
} else {
board[i][j] = 'X';
}
}
}
return; }
public void dfs(char[][] board, int m, int n) {
if (board[m][n] != 'O') return;
board[m][n] = '1';
if (m < board.length - 2)
dfs(board, m + 1, n);
if (m > 1)
dfs(board, m - 1, n);
if (n < board[0].length - 2)
dfs(board, m, n + 1);
if (n > 1)
dfs(board, m, n - 1); }
}

leetcode 130. Surrounded Regions----- java的更多相关文章

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

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

  2. Java for LeetCode 130 Surrounded Regions

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

  3. Leetcode 130. Surrounded Regions

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

  4. leetcode 130 Surrounded Regions(BFS)

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

  5. Leetcode 130 Surrounded Regions DFS

    将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...

  6. 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 用了第一种方式, ...

  7. 130. Surrounded Regions(M)

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

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

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

  9. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

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

  10. 【leetcode】Surrounded Regions

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

随机推荐

  1. Android Phonebook编写联系人UI加载及联系人保存流程(二)

    2014-01-06 17:18:29 1. Phonebook中新建/编辑联系人的UI不是用xml文件写的,它是随着帐号类型的改变来加载不同的UI,比如SIM联系人,只有Name.Phone Num ...

  2. HDU 3966 基础树链剖分

    题意:给一棵树,并给定各个点权的值,然后有3种操作:I C1 C2 K: 把C1与C2的路径上的所有点权值加上KD C1 C2 K:把C1与C2的路径上的所有点权值减去KQ C:查询节点编号为C的权值 ...

  3. SharePoint 2013 Nintex Workflow 工作流帮助(七)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 11. Check out item(Libraries and lists分组) 与上一个对应,用于签出条目.如果一个 ...

  4. Ajax返回类型JSON,XML

    Ajax的三种返回类型 **一.TEXT *二.JSON 数据显示页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...

  5. lightoj1027

    //Accepted 1688 KB 0 ms //概率简单题 //假设我们在n个门前加个起点,在n个门后加个终点,起点可以到达n个门, //为正的门可以到达终点,为负的回到起点 //则假设我们从起点 ...

  6. hdu 1025 dp 最长上升子序列

    //Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...

  7. Canopy使用教程 (2)

    1.下载https://reputation.alienvault.com/reputation.data alienvault公司的IP信誉数据库文件到本地,手动或者wget 2.使用 read_c ...

  8. C语言 约瑟夫圈问题:N个人围成一圈,从第一个人开始按顺序报数并编号1,2,3,……N,然后开始从第一个人转圈报数,凡是报到3的退出圈子。则剩下的最后一个人编号是多少。

    样例输入3  输出2 输入100   输出91 代码及分析: #include<stdio.h> int main() { int i,n,N,out,a[1000]; out=i=n=0 ...

  9. mahout中kmeans算法和Canopy算法实现原理

    本文讲一下mahout中kmeans算法和Canopy算法实现原理. 一. Kmeans是一个很经典的聚类算法,我想大家都非常熟悉.虽然算法较为简单,在实际应用中却可以有不错的效果:其算法原理也决定了 ...

  10. Windows下adb push 总是提示Failed to copy "XX.apk" to 'system/app':Read-only file system

    一般情况看到这种提示我们会想到需要root权限,然后敲上adb remount,但是当我们执行过adb remount后,提示成功,但执行push命令依旧无法完成push. 那么此时我们的做法应该是重 ...