在一个由 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. 《Python之BMI计算》

    <Python之BMI计算> 前段时间写了个 BMI 因为刚刚开始学 有几个错误 第一个: 厘米我当时也没注意因为觉得去掉0.00的话后面1866666666是正确的BMI值 刚刚去看看去 ...

  2. Django高级实战 开发企业级问答网站✍✍✍

    Django高级实战 开发企业级问答网站 1. 创建项目与app 创建项目 django-admin startproject firstsite 创建app python manage.py sta ...

  3. LeetCode 8.字符串转换整数 (atoi)(Python3)

    题目: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该 ...

  4. 如何快速合并多个TXT文本内容

    工作中有时候需要合并很多文本内容,例如一些推送清单之类,一个一个打开去复制粘贴的话,少量还行,如果txt文本数据量大(10+M以上)且文件数量多(成百上千),这种方式就显得很低效了.具体要求如下:   ...

  5. Atcoder arc085

    C:HSI 期望模型,不想说. #include<cstdio> using namespace std; typedef long long ll; int main() { int n ...

  6. 解决mysql中无法修改事务隔离级别的问题

    使用SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;修改数据库隔离级别, 然后执行SELECT @@TX_ISOLATION;后发现数据库的隔离级别并 ...

  7. BZOJ1912:[APIO2010]patrol巡逻

    Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...

  8. PAT甲级——【牛客练习题1002】

    题目描述 Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chi ...

  9. 《DSP using MATLAB》Problem 8.13

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  10. Synchronized理解及用法

    加锁: 1.同步实例方法,锁是当前实例对象 2.同步类方法,锁的是当前类对象 3.同步代码块,锁是括号里面的对象 原理: JVM内置锁通过synchronized使用,通过内部对象Monitor(监视 ...