在一个由 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. NSString 是否存在空格

    NSString *_string = [NSString stringWithFormat:@"123 456"]; NSRange _range = [_string rang ...

  2. performSegueWithIdentifier 不生效的解决办法

    相信很多人都会遇到这样的需求: APP 打开以后,判断用户是否登录,如果未登录,就跳转到登陆页. 今天我也遇到了这个需求,发现我封装的一个 `func checkLoginStatus()` 放在 ` ...

  3. P1290-关灯

    描述 Description 宁智贤得到了一份有趣而高薪的工作.每天早晨她必须关掉她所在村庄的街灯.所有的街灯都被设置在一条直路的同一侧.宁智贤每晚到早晨5点钟都在晚会上,然后她开始关灯.开始时,她站 ...

  4. adb server version (39) doesn't match this client (40); killing...

    在启动RN项目的时候也报错,上面的错误是在adb的环境变量中的位置和android studio的sdk不是一个位置.adb是在sdk中的,所以他们应该是一致的位置 android studio的sd ...

  5. 集成Activiti工作流的J2EE快速开发框架

    框架简介 enos款快速开发模块化脚手架,实现功能有系统模块:菜单管理.用户管理.角色管理,系统监控:系统日志.接口api.sql监控. 系统功能 系统管理:菜单管理.用户管理.角色管理 统一查询 p ...

  6. Hive_Hive的管理_web界面方式

    端口:9999启动方式: hive --service hwi &通过浏览器访问:http://<IP地址>:9999/hwi/ 执行启动命令后,报错,找不到hive-hwi-*. ...

  7. MFC 创建UI线程

    对于windows来说,所有的线程都是一样的,但MFC却把线程区分为两种:用户界面(UI)线程和工作者线程.用户界面线程具有消息循环而工作者线程没有.UI线程可以创建窗口并给这些窗口发送消息,工作者线 ...

  8. C#私有的构造函数的作用

    C#私有的构造函数的作用:当类的构造函数是私有的时候,也已防止C1 c1=new C1();实例化类.常见的应用是工具类和单例模式. using System;using System.Collect ...

  9. pay-spring-boot 开箱即用的Java支付模块,整合支付宝支付、微信支付

    关于 使用本模块,可轻松实现支付宝支付.微信支付对接,从而专注于业务,无需关心第三方逻辑. 模块完全独立,无支付宝.微信SDK依赖. 基于Spring Boot. 依赖Redis. 我能做什么 支付宝 ...

  10. 自定义orgmode中加粗字体的颜色

    自定义orgmode中加粗字体的颜色 Table of Contents 1. orgmode中加粗字体的默认处理 2. 设置设置加粗字体的颜色 1 orgmode中加粗字体的默认处理 在orgmod ...