首页 > 代码库 > leetcode statck

leetcode statck

1.从外围搜索O,深度搜索出现了
Line 35: java.lang.StackOverflowError
Last executed input:["OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
 1 public class Solution { 2     public void solve(char[][] board) { 3         if(board.length==0) return; 4         int len1=board.length; 5         int len2=board[0].length; 6         for(int i=0;i<len1;i++) 7         { 8             if(board[i][0]==‘O‘) dfs(board,i,0); 9             if(board[i][len2-1]==‘O‘) dfs(board,i,len2-1);10             11         }12         for(int i=0;i<len2;i++)13         {14             15             if(board[0][i]==‘O‘)dfs(board,0,i);16             if(board[len1-1][i]==‘O‘)dfs(board,len1-1,i);17         }18         for(int i=0;i<len1;i++)19         {20             for(int j=0;j<len2;j++)21             {22                 if(board[i][j]==‘h‘) board[i][j]=‘O‘;23                 else  board[i][j]=‘X‘;24             }25         }26         27         28     }29     public void dfs(char b[][],int i,int j)30     {31         if(i<0||i>b.length-1||j<0||j>b[0].length-1) return;32         if(b[i][j]!=‘O‘) return;33         34           if(b[i][j]==‘O‘) b[i][j]=‘h‘;35           dfs(b,i+1,j);//up36           dfs(b,i-1,j);//down37           dfs(b,i,j+1);//left38           dfs(b,i,j-1);//right;39          40         41     }42 }
View Code

2.广度搜索一定可以了。抽空在写,