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. CentOS 相关软件安装 与 Siege压测

    1.登录linux机器 a.打开mac的钥匙串 b.ssl root@ip c.输入密码 常用命令: cat /proc/version  查看系统版本 cat /etc/issue control ...

  2. java单例模式之懒汉式分析

    转自:http://blog.csdn.net/withiter/article/details/8140338 今天中午闲着没事,就随便写点关于Java单例模式的.其实单例模式实现有很多方法,这里我 ...

  3. CSS 分组和嵌套选择器

    CSS 分组和嵌套选择器 一.分组选择器 在样式表中有很多具有相同样式的元素. h1 { color:green; } h2 { color:green; } p { color:green; } 为 ...

  4. 20145303 实验一 Java开发环境的熟悉(Linux + Eclipse)

    20145303 实验一 Java开发环境的熟悉(Linux + Eclipse) 实验题目(4):实现学生成绩管理功能,并进行测试 思路: 对于实现学生成绩管理(student performanc ...

  5. 网络攻防工具介绍——Metasploit

    Metasploit 简介 Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,提供真正的安全风险情报.这些 ...

  6. bzoj 3450: Tyvj1952 Easy

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 411  Solved: 309[Submit][Status][Discuss] Descriptio ...

  7. SaltStack本地管理无master模式-第八篇

    Salt本地管理应用场景 1.在边缘节点服务器非常少没有Salt-master 2.零售店,电影院等弱网络环境没有Salt-master 3.快速部署单个服务没有Salt-master 实现 一,安装 ...

  8. 在Windows下使用Navicat连接Linux下的MySql

    Linux下的Mysql安装可以参考菜鸟教程:https://www.runoob.com/mysql/mysql-install.html 安装完成后你会发现用Navicat链接MySql会失败,这 ...

  9. 【bzoj5177】[Jsoi2013]贪心的导游(分块)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5177 在网上看到的题解基本都是用主席树,也就是带点骚操作的暴力直接艹过去的.这里分享一 ...

  10. hiho 1318 非法二进制数 dp

    #1318 : 非法二进制数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 如果一个二进制数包含连续的两个1,我们就称这个二进制数是非法的. 小Hi想知道在所有 n 位 ...