Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

在给定的二维字符数组,找出最大全为1的矩形包含的1的个数:

注意这里相当于求面积而已,只要求出边长就很好办了。边长用dp很好求,这题其实跟前面有道dp问题很像,以后有时间找出来,先贴下代码:

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

LeetCode OJ:Maximal Square(最大矩形)的更多相关文章

  1. [LeetCode] 85. Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

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

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

  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. Java for 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 ...

  5. (medium)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 ...

  6. [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

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

  8. Leetcode 221. Maximal Square

    本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...

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

  10. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

随机推荐

  1. 漂亮的输出-----prettytable和colorama的使用

    Python通过prettytable模块将输出内容如表格方式整齐输出,python本身并不内置,需要独立安装该第三方库. 1 pip install PrettyTable 1 #源码安装 2 wg ...

  2. 注意:PHP7中十个需要避免的坑

    1.不要使用mysql_函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP7已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mysqli_函数,或者更灵活的PDO实现. ...

  3. RocEDU.阅读.写作《苏菲的世界》书摘(四)

    亚理斯多德认为,快乐有三种形式.一种是过着享乐的生活,一种是做一个自由而负责的公民,另一种则是做一个思想家与哲学家.接着,他强调,人要同时达到这三个标准才能找到幸福与满足. 亚理斯多德提倡所谓的&qu ...

  4. SVN一直提示需要clean up

    无论到那一级都提示clean up, 这是陷入clean up 死循环的结果. 解决办法: 使用任何一款可以连sqllit 的数据库管理软件例如(Navicat Premium),连入 项目跟目录/. ...

  5. gerrit代码审核工具之“error unpack failed error Missing unknown”错误解决思路

    使用gerrit代码审核工具时遇到error: unpack failed: error Missing unknown d6d7c89bd1d77f44c5c8e99437aaffbfc0684e7 ...

  6. Jenkins FreeStyle or Pipeline Job

    FreeStyle Job: 1. 需要在页面添加模块配置项与参数完成配置 2. 每个Job仅能实现一个开发功能 3. 无法将配置代码化,不利于Job配置迁移与版本控制 4. 逻辑相对简单,无额外学习 ...

  7. [BZOJ2282]消防

    Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家 ...

  8. struts2的refreshModelBeforeResult

    首先想介绍的是struts2的原型驱动ModelDriven机制. 所谓的ModelDriven,就是把一个实体类当成页面数据的收集对象.用法看起来像下面这个样子 <span style=&qu ...

  9. linux top 各个标识的含义 详解

        top之前一直都是一知半解,今天周末加班,我的工作已经完成,在等同事吃饭,就把这个写下来. 第一行: top - 20:42:47 up 57 days,  1:25,  4 users,  ...

  10. spark(三)从hbase取数据

    前言 通过spark获取hbase数据的过程中,遇到了InputFormat.文章主要围绕InputFormat介绍.会牵扯到spark,mapreduce,hbase相关内容 InputFormat ...