leetcode 130. Surrounded Regions----- java
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的更多相关文章
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- leetcode 130 Surrounded Regions(BFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 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 ...
- 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 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [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 ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- Oracle性能调优
这部分目前主要是从网上搜集来的,后续要在实践中慢慢体会. v$sqltext: 存储的是被分割的sql v$sqlarea: 存储的是完整的sql和一些统计信息,比如累计的执行次数.逻辑读.物理读等( ...
- IT公司100题-15-求二元查找树的镜像
问题描述: 输入一颗二元查找树,将该树转换为它的镜像树,即对每一个节点,互换左右子树. 例如输入: 6/ \4 12/ \ / \2 5 8 16 输出: 6/ ...
- SQL Server 2005 镜像构建手册
转载:http://www.cnblogs.com/killkill/archive/2008/05/23/1205792.html 一. 镜像简介 1. 简介 数据库镜像是将数据库事务处理从一个SQ ...
- 标准库源码--wsgi服务器
功能模块化带来可自由组装的便利: 使用python的mixin特性装配 class ThreadHTTPServer(ThreadingMixin, HTTPServer): pass 上面表示1个请 ...
- 读者写者问题(有bug 后续更改)
与上一篇<秒杀多线程第十篇 生产者消费者问题>的生产者消费者问题一样,读者写者也是一个非常著名的同步问题.读者写者问题描述非常简单,有一个写者很多读者,多个读者可以同时读文件,但写者在写文 ...
- Oracle Data Integrator 12c (12.1.2)新特性
改进特性如下: 基于流程界面的声明式设计 在12c中,以前的接口(interface)已经改为映射(mapping),新的基于流程声明的设计方式更灵活,也更容易使用.在12c中,映射的实现是通过使用J ...
- 《c语言全局变量的用法》
//全局变量的用法. /*有一个一维数组,内放n个学生的成绩,(n由用户自己指定,通过调用函数实现定义一个数组.)写一个函数,当主函数调用此函数后,能求出平均分,最高分,最低分.*/ #include ...
- mysql innoDB 与 myISAM
转载文章 出处 http://www.pureweber.com/article/myisam-vs-innodb/ 使用MySQL当然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都 ...
- golang函数调用计时
package main import ( "log" "time" ) func f() { defer timeoutCheck("f slow& ...
- 需求分析(NABC)
团队开发需求分析 队长:郭庆樑 成员:林彦汝.张金 经过讨论,我们决定做一个基于Windows的小游戏——躲避小球. 把这个项目实现,组长强调有两点: 1.可实现:2.有用户. 可以说,我们最大的特点 ...