leetcode shttps://oj.leetcode.com/problems/surrounded-regions/
| Line 35: java.lang.StackOverflowError | |
| Last executed input: | ["OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO |
public class Solution {
public void solve(char[][] board) {
if(board.length==0) return;
int len1=board.length;
int len2=board[0].length;
for(int i=0;i<len1;i++)
{
if(board[i][0]=='O') dfs(board,i,0);
if(board[i][len2-1]=='O') dfs(board,i,len2-1);
}
for(int i=0;i<len2;i++)
{
if(board[0][i]=='O')dfs(board,0,i);
if(board[len1-1][i]=='O')dfs(board,len1-1,i);
}
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
if(board[i][j]=='h') board[i][j]='O';
else board[i][j]='X';
}
}
}
public void dfs(char b[][],int i,int j)
{
if(i<0||i>b.length-1||j<0||j>b[0].length-1) return;
if(b[i][j]!='O') return;
if(b[i][j]=='O') b[i][j]='h';
dfs(b,i+1,j);//up
dfs(b,i-1,j);//down
dfs(b,i,j+1);//left
dfs(b,i,j-1);//right;
}
}
2.广度搜索一定可以了。抽空在写,(可惜不行,我太水了超时代码)
class node
{
int x;
int y;
char c;
node(int x1,int y1,char c1)
{
x=x1;
y=y1;
c=c1; }
} public class Solution {
public void solve(char[][] board) {
if(board.length==0) return;
int len1=board.length;
int len2=board[0].length;
for(int i=0;i<len1;i++)
{
if(board[i][0]=='O') bfs(board,i,0);
if(board[i][len2-1]=='O') bfs(board,i,len2-1); }
for(int i=0;i<len2;i++)
{ if(board[0][i]=='O')bfs(board,0,i);
if(board[len1-1][i]=='O')bfs(board,len1-1,i);
}
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
if(board[i][j]=='h') board[i][j]='O';
else board[i][j]='X';
}
} }
public boolean is(int i,int j,int len1,int len2,char c[][])
{
if(i>=0&&i<len1&&j>=0&&j<len2&&c[i][j]=='O') return true;
return false;
} //put the o to h
public void bfs(char b[][],int i,int j)
{
int len1=b.length;
int len2=b[0].length;
Queue<node> queue=new LinkedList<node>(); queue.offer(new node(i,j,b[i][j]));
while(!queue.isEmpty())
{
node n1=queue.poll();
b[n1.x][n1.y]='h'; if(is(n1.x,n1.y+1,len1,len2,b))
{ queue.offer(new node(n1.x,n1.y+1,b[n1.x][n1.y+1]));
}
if(is(n1.x,n1.y-1,len1,len2,b))
{ queue.offer(new node(n1.x,n1.y-1,b[n1.x][n1.y-1]));
}
if(is(n1.x+1,n1.y,len1,len2,b))
{ queue.offer(new node(n1.x+1,n1.y,b[n1.x+1][n1.y]));
}
if(is(n1.x-1,n1.y,len1,len2,b))
{ queue.offer(new node(n1.x-1,n1.y,b[n1.x-1][n1.y]));
} } } }
leetcode shttps://oj.leetcode.com/problems/surrounded-regions/的更多相关文章
- leetcode https://oj.leetcode.com/problems/jump-game-ii/
1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...
- 【LeetCode OJ】Surrounded Regions
Problem Link: http://oj.leetcode.com/problems/surrounded-regions/ We can do follows in the 2D board. ...
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)
Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- 面向对象 ---Java抽象类
在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不能实例化对象之外, ...
- Oracle学习第三讲
关联查询 笛卡尔积 指做关联操作的每个表的每一行都和其他表的每一行组合,假设两个表的记录条数分别为x和y,笛卡尔积将返回x*y条记录 例如:select count(*) from emp; sele ...
- 关于vs2008使用oracleclient链接oracle数据库报报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用
用vs2008链接oracle数据库出现问题,报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用,从网上找了好久方法,有两种oracle客户端文件权限,和运行vs2008以管理 ...
- 删除svn密码方法
很多时候使用svn,我们需要切换svn账号,但是由于之前的账号已经选择了记住密码,那么我们应该如何删除svn密码来切换新的svn账号呢? 其实很简单,svn账号密码信息保存在电脑某一文件中,我们只要删 ...
- .NET生成静态页面例子
主要做法如下: 1.创建网站,并创建一个模板页,template.htm 2.添加一个web窗体Default.aspx 3.在网站下新建文件夹htm,设置该文件夹的属性,确保该文件夹具有可写权限 详 ...
- python学习笔记--随时更新
# coding=GBK score = 90 if score >= 80: print("好") elif score >= 60: print("及格& ...
- 图像处理简单实例[OpenCV 笔记1]
几个入门的简单程序,和对应的CMakeList, 虽然简单重新测一下写一下也是好的. CMake教程传送门 图像显示 ShowImage.cxx #include <opencv2/opencv ...
- juquery验证插件validation addMethod方法使用笔记
该方法有三个api接口参数,name,method,messages addMethod(name,method,message)方法 参数 name 是添加的方法的名字. 参数 method 是一个 ...
- oracle删除用户所有表
在删除数据表的时候往往遇到外键约束无法删除的情况,我们可以通过以下几步将数据库表删除,建议在删除库之前先对数据库进行备份,养成良好习惯. 1.删除外键 --查询用户所有表的外键,owner条件为use ...
- php 执行linux 命令函数
php的内置函数exec,system都可以调用系统命令(shell命令),当然还有passthru,escapeshellcmd等函数. 在很多时候利用php的exec,system等函数调用系统命 ...