【leetcode】com/problems/surrounded-regions/
dfs 栈溢出,bfs超时,用dfs非递归就不溢出了,前后写了1一个星期
class node
{
int i;
int j; public node(int i1,int j1)
{
i=i1;
j=j1; }
} public class Solution {
public void solve(char[][] board) { int len1=board.length;
if(len1<=0)return;
int len2=board[0].length;
if(len1<=2||len2<=2) return;
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 boolean is(int i,int j,int len1,int len2)
{
if(i>=0&&i<len1&&j>=0&&j<len2) return true;
return false; }
public void dfs(char b[][],int i,int j)
{
int len1=b.length;
int len2=b[0].length;
Stack<node> s=new Stack<node>();
b[i][j]='h';
s.push(new node(i,j)); }
}
【leetcode】com/problems/surrounded-regions/的更多相关文章
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】面试题13. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- [分词] C#SegList分词辅助类,帮助类 (转载)
点击下载 SegList.rar 主要功能如下最新的SegList分词辅助类,帮助类看下面代码吧 /// <summary> /// 类说明:SegList /// 编 码 人:苏飞 // ...
- ios专题 - 斯坦福大学iOS开发公开课总结
转自:http://blog.devtang.com/blog/2012/02/05/mvc-in-ios-develop/ 前言 iphone开发相关的教程中最有名的,当数斯坦福大学发布的”ipho ...
- 拥抱模块化的JavaScript
前言 我们再一次被计算机的名词.概念笼罩. Backbone.Emberjs.Spinejs.Batmanjs 等MVC框架侵袭而来.CommonJS.AMD.NodeJS.RequireJS.Sea ...
- 修正constructor的指向
function Aaa(){ } //Aaa.prototype.constructor = Aaa; //每一个函数都会有的,都是自动生成的 Aaa.prototype.name = '小明' ...
- DOM 操作内容 innerText/innerHTML
DOM 操作内容 innerText/innerHTML innerText属性(firefox不支持,可用 textContent)var div = document.getElementById ...
- angularjs入门整理
之前发过一篇博文,从mobile angular ui的demo和其官网初识整个angularjs的大体使用,但是没很好学习,只是通过一些技术博文初步认识,陷入很多坑.所以现在在中文官网正式整理下知识 ...
- Hierarchy Viewr 配合 adb 命令 查看窗口属性
Hierarchy Viewr 可以看到当前 的 窗口层次如下
- java 正则操作之获取
// 正则操作 获取import java.util.regex.*;class Demo{ public static void main(String[] args){ String str=& ...
- WebApi学习总结系列第三篇(Http)此篇持续更新...
越了解Http对WebApi开发就越有帮助,因为WebApi就是建立在Http基础之上的. 一.Http: 通过 <ASP.NET Web API 2 框架揭秘>一书中 了解到 什么叫We ...
- (MVC)验证用户是否登录 登录认证
验证类 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...