在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。

示例:

输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4

方法一:暴力(肯定超时)

方法二:

dp,

可以把dp[i][j]表示到i,j为止的正方形边长大小。

到i ,j的正方形大小是min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1。最小的那个就是正方形的最大边长大小

class Solution {
public:
int maximalSquare(vector<vector<char> >& matrix)
{
int r = matrix.size();
if(r == 0)
return 0;
int c = matrix[0].size();
vector<vector<int> > dp(r, vector<int>(c, 0));
int MAX = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(i == 0 || j == 0)
{
dp[i][j] = matrix[i][j] - '0';
MAX = max(MAX, dp[i][j]);
continue;
}
else if(matrix[i][j] == '1')
{
dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
MAX = max(MAX, dp[i][j]);
}
}
}
return MAX * MAX;
}
};

Leetcode221. Maximal Square最大正方形的更多相关文章

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

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

  4. 221 Maximal Square 最大正方形

    在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...

  5. leetcode221 Maximal Square

    思路: dp. 实现: class Solution { public: int maximalSquare(vector<vector<char>>& matrix) ...

  6. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  7. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  8. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

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

随机推荐

  1. R:ggplot2数据可视化——进阶(2)

    Part 2: Customizing the Look and Feel, 更高级的自定义化,比如说操作图例.注记.多图布局等  # Setup options(scipen=999) librar ...

  2. .net core模糊查询及分页

    在项目文件夹中,创建 PaginatedList类,然后用以下代码替换模板代码. using Microsoft.EntityFrameworkCore; using System; using Sy ...

  3. python语句结构(控制语句与pass语句)

    python语句结构(控制语句和pass语句) break-跳出循环:语句可以跳出for和while语句的循环体.如果你从for和while循环中终止,任何对应循环的else语块均终止 continu ...

  4. 控制类名(className 属性)设置或返回class属性

    控制类名(className 属性) className 属性设置或返回元素的class 属性. 语法: object.className = classname 作用: 1.获取元素的class 属 ...

  5. Error:(27, 13) Failed to resolve: com.android.support.constraint:constraint-layout:1.0.2约束布局constraint-layout导入失败的解决方案

    运行demo提示错误: Error:(27, 13) Failed to resolve: com.android.support.constraint:constraint-layout:1.0.2 ...

  6. Sky Code

    Sky Code 给出n个数,求选出4个数组合,使其gcd为1,,\(n<=10000\),每个数\(<=10000\). 解 理解1:容斥原理 注意到Mobius反演式子不好写出,于是我 ...

  7. layui相关用法总结

    1.关闭当前弹出层 parent.layer.close(parent.layer.getFrameIndex(window.name));

  8. 拦截器和自定义注解@interface

    1 .拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在Handler执行之前或之后加入某些操作,其实就是AOP的一种实现策略. 拦截用户的请求并进行相应的处理,比如:判断用 ...

  9. AnsiString, String, char,char

    AnsiString 是一个类,String 是一个结构,char* 是一个指针 .String是Pascal的类型原型,因为C++中没有字符串数据类型的,因此使用char*来存储,char*必须是以 ...

  10. Cannot find module '@babel/plugin-proposal-class-properties'

    cnpm install --save-dev @babel/plugin-proposal-class-properties