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 ...
随机推荐
- WIN7 VMware Ubuntu虚拟机连接secureCRT及ftp传输文件
一.安装vmware没有问题,网络设置以NAT方式(VMnet8是vmware专属的虚拟网卡).安装完毕后设置虚拟机的 VirtualNetworkEditor(可以再开始->所有程序VMwar ...
- 01-Objective-C
前言 目 前来说,Objective-C(简称OC)是iOS开发的核心语言,在开发过程中也会配合着使用C语言.C++,OC主要负责UI界面,C语言.C++ 可用于图形处理.近来,流传Ruby.C# ...
- html-----006
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【html】【3】html标签列表
必看参考: http://www.divcss5.com/html/h323.shtml http://www.w3school.com.cn/tags/tag_html.asp 常用: <ht ...
- Java之循环输出等腰三角形
public class aaa{ public static void main(String[] args) { int max=5; for(int i=1;i<=5;i++){//控制行 ...
- YII CRUD 例子
< ? php class PostTest extends CDbTestCase{ public $ fixtures = array ( 'posts' = > 'Post' , ' ...
- angularjs应用骨架(2)
时隔一个星期,接着上一篇的angularjs应用骨架继续聊聊angularjs其他的其他的内容. 区分UI和控制器的职责 在应用控制器中有三种职责: 1.为应用中模型设置初始状态 2.通过$scope ...
- ecshop---京东手机模板js的eval产生冲突的解决方法。
今天弄ecshop手机模板的时候,发现首页的广告图出不来,js报错
- ecshop 报错
ECShop出现Strict Standards: Only variables should be passed b (2014-06-04 17:00:37) 转载▼ 标签: ecshop 报错 ...
- linux之GDB常用命令汇总
查看gdb的版本号 (1)rpm -q gdb 会显示是否安装gdb及版本号 (2)gdb --version也可以 breakpoint b main; b 20; 设置断点 breakpoint ...