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. ubuntu 防火墙 添加策略 解决mysql远程访问问题

    ubuntu 的iptables 文件不在 init.d中 不能 service iptables restart 只修改 /etc/iptables 文件也不管用 sudo iptables -L ...

  2. C++-不要在构造和析构函数中调用虚函数

    在实习的单位搞CxImage库时不知为什么在Debug时没有问题,但是Release版里竟然跳出个Pure virtual function call error! 啥东西呀,竟然遇上了,就探个究竟吧 ...

  3. asp.net mvc 2.o 中使用JQuery.uploadify

    From:http://www.cnblogs.com/strugglesMen/archive/2011/07/01/2095916.html 官方网站http://www.uploadify.co ...

  4. URAL 1519 基础插头DP

    题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...

  5. K2 BPM+Microsoft Dynamics CRM,妥妥的~

    啊~~~~七夕 ▼ 你比巴西少一xi 你比山西多四xi 对有情人来说今天就是情人节,对单身汪来说,今天就是个星期四. but,软件也是要秀恩爱的! ♥ 晒晒我家亲爱的CRM,它的全名叫Microsof ...

  6. web安全测试-AppScan

    安全测试应该是测试中非常重要的一部分,但他常常最容易被忽视掉. 尽管国内经常出现各种安全事件,但没有真正的引起人们的注意.不管是开发还是测试都不太关注产品的安全.当然,这也不能怪我们苦B的“民工兄弟” ...

  7. Java运算符及顺序、选择结构

    :运算符(掌握) ()算术运算符 A:+,-,*,/,%,++,-- B:+的用法 a:加法 b:正号 c:字符串连接符 C:/和%的区别 数据做除法操作的时候,/取得是商,%取得是余数 D:++和- ...

  8. (转)Sqlite中INTEGER PRIMARY KEY AUTOINCREMENT和rowid的使用

    原文:http://www.cnblogs.com/peida/archive/2008/11/29/1343832.html Sqlite中INTEGER PRIMARY KEY AUTOINCRE ...

  9. Oracle数据库的引导过程

    Oracle在启动数据库:会先去读1号数据文件的文件头中记录的root dba, 再通过root dba去找bootstrap$中存储的那些数据字典的基表的定义,最后根据这些定义创建数字字典,即所谓的 ...

  10. sqlserver中自定义函数+存储过程实现批量删除

    由于项目的需要,很多模块都要实现批量删除的功能.为了方便模块的调用,把批量删除方法写成自定义函数.直接上代码. 自定义函数: ALTER FUNCTION [dbo].[func_SplitById] ...