LeetCode 最大正方形
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。
示例:
输入: 1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 输出: 4
解法:判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了。
我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情况是是它的上方,左方和左上方为右下角的正方形的大小都一样的,这样加上该点就可以构成一个更大的正方形。
但如果它的上方,左方和左上方为右下角的正方形的大小不一样,合起来就会缺了某个角落,这时候只能取那三个正方形中最小的正方形的边长加1了。
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty() || matrix[].empty())
return ;
int n=matrix.size(),m=matrix[].size();
int ans=;
int dp[n][m];
memset(dp,,sizeof(dp));
for(int i=;i<n;i++)
{
if(matrix[i][]=='')
{
dp[i][]=;ans=;
}
}
for(int i=;i<m;i++)
{
if(matrix[][i]=='')
{
dp[][i]=;ans=;
}
}
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(matrix[i][j]=='')
dp[i][j]=min(dp[i-][j-],min(dp[i-][j],dp[i][j-]))+;
ans=max(ans,dp[i][j]);
}
}
return ans*ans;
}
};
LeetCode 最大正方形的更多相关文章
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] 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] Valid Square 验证正方形
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [LeetCode] Magic Squares In Grid 网格中的神奇正方形
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...
- C#版 - Leetcode 593. 有效的正方形 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)
Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...
- C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形
上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...
- [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 ...
随机推荐
- Aufree/trip-to-iOS
https://github.com/Aufree/trip-to-iOS?utm_source=next.36kr.com
- 升级ruby的版本
升级ruby版本,有时候安装ruby的版本过低,需要进行升级,例如安装在centos6.7安装fpm需要ruby版本在1.9以上. 1.主机环境如下: [root@test ~]# cat /etc/ ...
- JavaScript引擎基本原理:Shapes和Inline Caches
原文链接: JavaScript engine fundamentals:Shapes and line Cahes 这篇文章描述了一些在js引擎中通用的关键点, 并不只是V8, 这个引擎的作者(Be ...
- java中代码执行顺序
静态代码块 -- >构造代码块 --> 构造方法静态代码块:只执行一次构造代码块:每次调用构造方法都执行 http://blog.csdn.net/wuhaiwei002/article/ ...
- String的内存和intern()方法
一.关于常量池 字符串在Java中用的非常得多,Jvm为了减少内存开销和提高性能,使用字符串常量池来进行优化. 在jdk1.7之前(不包括1.7),Java的常量池是在方法区的地方,方法区是一个运行时 ...
- 537 Complex Number Multiplication 复数乘法
详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...
- atomic用法
memory order 源码变成可执行程序,一般由预编译,编译,汇编,链接.源码重排序一般分为编译期重排序和运行期重排序. 编译期重排序:编译器在不改变单线程程序的语义的前提下,可以重新安排语句的执 ...
- jQuery取得/设置select的值
本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...
- php中socket的使用(重点参考)
一.开启socket phpinfo();查看是否开启了socket扩展,否则在php.ini中开启. 二.服务器端代码的写法 <?php error_reporting(E_ALL); set ...
- [异常]undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>
在进行Rspec 编译测试: bundle exec rspec spec/requests/static_pages_spec.rb 提示错误: FF Failures: 1) Static pag ...