[Leetcode221]最大面积 Maximal Square
【题目】
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example:
Input: 1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 Output: 4
【思路】
- dp square面积和三个有关
- 注意特殊空集条件
【代码】
class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix.length==0)
return 0;
int dp[][]=new int[matrix.length+1][matrix[0].length+1];
int ans=0;
for(int i=1;i<matrix.length+1;i++){
for(int j=1;j<matrix[0].length+1;j++){
if(matrix[i-1][j-1]=='1'){
dp[i][j]=Math.min(Math.min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
ans=Math.max(ans,dp[i][j]);
}
}
}
return ans*ans;
}
}
[Leetcode221]最大面积 Maximal Square的更多相关文章
- [Swift]LeetCode221. 最大正方形 | Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- 动态规划-最大的正方形面积 Maximal Square
2018-09-13 19:19:44 问题描述: 问题求解: 方法一: 使用动态规划来求解,算法时间复杂度O(n^2). dp[i][j] : 以(i, j)为右下角的面积最大的正方形的边长. 初始 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [LintCode] 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
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
随机推荐
- Day1-Request/BeautifulSoup
requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...
- 单细胞RNA-seq比对定量用什么工具好?使用哪个版本的基因组?数据来说话
这么多工具和基因组版本,选择困难症犯了,到底用哪个好呢? 2018 nature - Developmental diversification of cortical inhibitory inte ...
- Shell脚本中的并发(转)
转自http://blog.csdn.net/wangtaoking1/article/details/9838571 主要记录一下Shell脚本中的命令的并发和串行执行. 默认的情况下,Shell脚 ...
- android -------- 安装APK报错:Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE解决方法
记录一个 DELETE_FAILED_INTERNAL_ERROR Error while Installing APK问题 之前遇到这个问题 方案1 将data/data/目录下该应用的包名的目录删 ...
- 微信小程序页面内转发 按钮 转发
通过给 button 组件设置属性 open-type="share",可以在用户点击按钮后触发 Page.onShareAppMessage() 事件,如果当前页面没有定义此事件 ...
- PHP引用赋值
<?php/** * 在PHP 中引用的意思是用不同的名字访问同一个变量内容 * 只有有名字的变量才可以引用赋值,否则会报错 * 引用赋值 不是在内存上同体,只是把各自的值关联起来 * unse ...
- 0.1.2 max_element和min_element的用法
找到的位置都是第一个最大(小)的元素,即存在多个相同大小的元素的时候找到的是第一个. 返回的是指针(元素地址). printf("%d\n",*max_element(a,a+n) ...
- ubuntu18.04安装jdk1.8.0_11并配置环境变量.md
参考:https://www.jianshu.com/p/95f075761dc0 由于安装文件免安装程序,故只需要将对应文件复制到相应目录,然后配置环境变量即可: 1.移动文件到指定目录 (1)在/ ...
- SWUST OJ(955)
单链表上查找算法的实现 #include <stdio.h> #include <stdlib.h> typedef struct LinkNode //单链表节点结构的定义 ...
- 创建springboot的聚合工程(一)
比起传统复杂的单体工程,使用Maven的多模块配置,可以帮助项目划分模块,鼓励重用,防止POM变得过于庞大,方便某个模块的构建,而不用每次都构建整个项目,并且使得针对某个模块的特殊控制更为方便.接下来 ...