在一个由 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 最大正方形的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  2. [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 ...

  3. [LeetCode] Valid Square 验证正方形

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  4. [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 ...

  5. C#版 - Leetcode 593. 有效的正方形 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  6. 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 ...

  7. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  8. C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

    上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...

  9. [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 ...

随机推荐

  1. Window 10 Alt Tap 切换窗口,设置成Windows 7风格

    升级了Windows 10 以后,切换窗口非常的难受.新版的窗口切换的图标变成了窗口预览了,这总让我一瞬间找不到要去的窗口,所以我打算切换成Windows 7 的小图标风格. Windows + R ...

  2. 调用Web API将文件上传到服务器的方法(.Net Core)

    最近遇到一个将Excel通过Web API存到服务器的问题,其中涉及到Excel的读取.调用API.Web  API怎么进行接收. 一. Excel的读取.调用API Excel读取以及调用API的代 ...

  3. DB2 学习--(1)--安装教程

    db2 linux 安装部署 1 解压文件 tar -zxvf db2_v101_linuxx64_expc.tar.gz 2 切换路径 cd expc/ 3 启动安装程序 ./db2_install ...

  4. Mysql相关函数使用和总结(cast、convert)

    一.类型转换 1.获取一个类型的值,并产生另一个类型的值,CAST()和CONVERT()函数. 用法: CAST(value as type); CONVERT(value, type); 解释:C ...

  5. [题解](gcd/lcm)luogu_P1072_Hankson的趣味题(NOIP2009)

    连续三次不开longlong导致wa!!! 不开longlong一时爽,一会提交火葬场!!! OI千万条,longlong第一条 乘法不longlong,提交两行泪 暴力luogu就能过了???打好暴 ...

  6. Linux (三)

    chmod :用来改变文件或目录的访问权限 语法格式:chmod [参数选项] [mode][文件或者目录] 参数选项: -v :显示权限变更的详细信息 -R :对当前目录下的所有文件以及子目录进行相 ...

  7. Codeforces Round #396 (Div. 2) E

    Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirec ...

  8. Unity Shader入门精要学习笔记 - 第8章 透明效果

    转载自 冯乐乐的 <Unity Shader入门精要> 透明是游戏中经常要使用的一种效果.在实时渲染中要实现透明效果,通常会在渲染模型时控制它的透明通道.当开启透明混合后,当一个物体被渲染 ...

  9. (转)Unity3D中常用的数据结构总结与分析

    http://www.cnblogs.com/murongxiaopifu/p/4161648.html#array   1.几种常见的数据结构 常碰到的几种数据结构:Array,ArrayList, ...

  10. mybatis(错误) 项目启动时报“Result Maps collection already contains value forxxx”的解决方案

    使用逆向工程生成代码时,一定要将原来的代码删除干净,如果覆盖的话,不是真正的覆盖,在原来的代码上增加重复的代码,导致出错