Leetcode221. Maximal Square最大正方形
在一个由 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最大正方形的更多相关文章
- [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 ...
- [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] 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 ...
- 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 ...
- leetcode221 Maximal Square
思路: dp. 实现: class Solution { public: int maximalSquare(vector<vector<char>>& matrix) ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 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 ...
随机推荐
- AQS(队列同步器)
目录导引: 一.简介 二.源码解析(JDK8) 三.运用示例 一.简介 1.volatile volatile修饰的共享变量可以保证可见性和有序性(禁止指令重排序). 2.CAS: CAS的原理很简单 ...
- 01_springboot2.x之springboot入门
1.简介 Spring Boot来简化Spring应用开发,约定大于配置, 去繁从简,just run就能创建一个独立的,产品级别的应用. 优点: 1.简化Spring应用开发的一个框架: 2.整个S ...
- C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换
定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 复制代码 代码如下: (1)byte[] bt=System.Text.Encoding.Default.GetB ...
- python语句结构(range函数)
python语句结构(range函数) range()函数 如果你需要遍历数字序列,可以使用内置range()函数,它会生成序列 也可以通过range()函数指定序列的区间 也可以使用range()函 ...
- 2018-8-10-使用-ahk-让普通键盘变为Dvorak键盘
title author date CreateTime categories 使用 ahk 让普通键盘变为Dvorak键盘 lindexi 2018-08-10 19:16:51 +0800 201 ...
- 【CF622F】The Sum of the k-th Powers (拉格朗日插值法)
用的dls的板子,因为看不懂调了好久...果然用别人的板子就是这么蛋疼- -|| num数组0~k+1储存了k+2个值,且这k+2个值是自然数i的k次方而不是次方和,dls的板子自己帮你算和的...搞 ...
- 串口通信中,QString 、QByteArray 转化需要注意的问题
在做串口通信的时候,其中犯了一个错误.在此记录一下:QT中串口通信接到收据和发送数据的接口如下: QByteArray QIODevice::readAll()//接受数据 qint64 QIODev ...
- MySQL中\g和\G的作用
\g的作用和MySQL中的分号”;"是一样: \G的作用是讲查找到的内容结构旋转90度,变成纵向结构: 下面举例说明,查找数据库中的存在的存储过程状态: SHOW PROCEDURE STA ...
- 【JZOJ3238】【BZOJ3482】超空间旅行
description 在遥远的未来,行星之间的食品运输将依靠单向的贸易路线.每条路径直接连接两个行星,且其运输时间是已知的. 贸易商协会打算利用一项最近发现的新技术--超空间旅行,以增加一些新的航线 ...
- String方法之fromCharCode()和charCodeAt()
1.fromCharCode fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串. 语法 我们可以根据 Unicode 来输出 "HELLO" ...