Java for LeetCode 221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
解题思路:
dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候的最大矩形的边长,JAVA实现如下:
public int maximalSquare(char[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0)
return 0;
int res = 0;
int[] dp = new int[matrix[0].length];
for (int i = 0; i < matrix[0].length; i++)
if (matrix[0][i] == '1'){
dp[i] = 1;
res=1;
}
for (int i = 1; i < matrix.length; i++) {
dp[0] = matrix[i][0] == '1' ? 1 : 0;
res=Math.max(res, dp[0]);
for (int j = 1; j < matrix[0].length; j++) {
if(matrix[i][j]=='0')
dp[j]=0;
else if (dp[j] == dp[j - 1]
&& matrix[i - dp[j]][j - dp[j]] == '0') {
dp[j] = Math.min(dp[j], dp[j - 1]);
} else
dp[j] = Math.min(dp[j], dp[j - 1]) + 1;
res=Math.max(res, dp[j]);
}
}
return res*res;
}
Java for LeetCode 221 Maximal Square的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [LeetCode] 221. Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- (medium)LeetCode 221.Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- Leetcode 221. Maximal Square
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
随机推荐
- UML类图之类与类的关系
类与类之间的关系是在需求分析阶段确定的. 类与类之间的关系.有一般化关系.关联关系.聚合关系.合成关系和依赖关系. 1.一般化关系表示类与类之间的继承关系,接口与接口之间的继承关系,或类对接口的实现关 ...
- Jcrop+uploadify+php实现上传头像预览裁剪
最近由于项目需要,所以做了一个上传头像预览并且可以预览裁剪的功能,大概思路是上传的图片先保存到服务器,然后通过ajax从服务器获取到图片信息,再利用Jcrop插件进行裁剪,之后通过PHP获取到的四个裁 ...
- Elven Postman(BST )
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- Hadoop 面试题 之Hive
1.Hive 有哪些方式保存元数据,各有哪些特点. 15. Hive内部表和外部表的区别 23.hive底层与数据库交互原理Hive的Hql语句掌握情况? 36.使用Hive或自定义mr实现如下逻辑: ...
- java执行顺序
本文讨论Java中(静态)变量.(静态)代码块的执行顺序 首先创建3个类: 1.Foo类,用于打印变量 public class Foo { public Foo(String word) { Sys ...
- 格式化Double类型
//格式化Double类型 //F:默认是2位小数点 //F6:输出小数点后6位,不够的话用0补齐 //G:默认输出原先的,保留小数点后面的位数 LalTotal.Text = "合计:原始 ...
- linux 驱动 工作队列
http://blog.sina.com.cn/s/blog_78d30f6b0102uyaf.html http://blog.csdn.net/lyc_stronger/article/detai ...
- Lua模块测试
Lua模块 ---------------------------------------------------------- ----------------------- 模块测试module_ ...
- [转]CentOS 5.3通过yum升级php到最新版本的方法
来自:www.jasonlitka.com/media 通过测试,方法三可行: 方法三 vim /etc/yum.repos.d/utterramblings.repo 输入 [utterrambli ...
- Android 网络请求框架android-async-http问题
今天通过接口请求服务器的一些app数据,发现一个很奇怪的问题,请求一个链接的时候,通常在第一次请求发起的时候没有什么问题,能很快的拿到数据,但是 往后再去请求的时候就会等待很久,而且最后会请求失败,一 ...