public static int maximalRectangle(char[][] matrix) {

		 int rowNum=matrix.length;
if(rowNum==0)
return 0;
int columnNum=matrix[0].length; int[][] height=new int[rowNum][columnNum+1];
int maxarea=0; for(int i=0;i<rowNum;i++)
{
for(int j=0;j<columnNum;j++)
{
int k=i;
height[i][j]=0;
while(k>=0&&j<columnNum)
{
if(matrix[k][j]=='1')
height[i][j]++;
else
break;
k--;
} }
height[i][columnNum]=-1;
} Stack<Integer> stack=new Stack<>();
for(int i=0;i<rowNum;i++)
{
for(int j=0;j<=columnNum;j++)
{
int a=height[i][j];
int b=stack.isEmpty()?-1:stack.peek();
if(stack.isEmpty()||height[i][j]>=height[i][stack.peek()])
stack.push(j);
else
{ int tempPop=stack.pop();
maxarea=Math.max(maxarea, height[i][tempPop]*(stack.isEmpty()? j:j-1-stack.peek()));
j--;
}
}
stack.clear();
} return maxarea; }

题目:

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

也就是说,给一个仅仅有0和1的2维矩阵,求当中1可以组成的最大四方形面积

解题:

这题能够看作是前面一题(LeetCode84)的拓展,前面一题中输入的是一个数组,数组每个元素的值看作的矩形的高度,在这题中先对矩阵做一个处理。对矩阵的元素计算其高度,处理完之后得到一个每个原矩阵元素的高度矩阵,把这个矩阵当作输入就和前面一题是类似了。

代码:

LeetCode85 Maximal Rectangle java题解的更多相关文章

  1. 85. Maximal Rectangle (JAVA)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  2. leetcode85 Maximal Rectangle

    思路: 分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算. 实现: # ...

  3. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

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

  5. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  6. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  7. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  8. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

  9. 47. Largest Rectangle in Histogram && Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. Welcome-to-Swift-17自判断链接(Optional Chaining)

    自判断链接(Optional Chaining)是一种可以请求和调用属性.方法及子脚本的过程,它的自判断性体现于请求或调用的目标当前可能为空(nil).如果自判断的目标有值,那么调用就会成功:相反,如 ...

  2. 【bzoj4568】[Scoi2016]幸运数字 树上倍增+高斯消元动态维护线性基

    题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征.一些旅行者希望游 ...

  3. kb-07线段树--10--dfs序建树

    /* hdu3974 dfs序建树,然后区间修改查询: */ #include<iostream> #include<cstdio> #include<cstring&g ...

  4. POJ——1195Mobile phones(二维树状数组点修改矩阵查询)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17764   Accepted: 8213 De ...

  5. [OJ#63]树句节够提

    [OJ#63]树句节够提 试题描述 给定一棵节点数为 N 的有根树,其中 1 号点是根节点,除此之外第 i 个节点的父亲为 fi.每个节点有一个权值 Ai,所有边权均为 1. 给定 Q 个询问,每个询 ...

  6. 将selenium集成到scrapy框架中

    一 首先想到的是将selenium 写在下载中间件的process_request中.如以下代码. middleware.py from selenium import webdriver from ...

  7. nvm、node、npm安装以及pycharm配置eslint

    nvm.node.npm之间的区别 1. nvm的官方叫法:nodejs版本管理工具. nvm相当于是家长,一个家长可以管理多个孩子. 也就是说:一个nvm可以管理很多node版本和npm版本. 2. ...

  8. 《Vim实用技巧》第2版读书笔记

    学习前的建议: 一.如果你需要一个功能强大的文本编辑器,速度飞快.使用方便.代码补全.编程强大.定制性强,Vim就是你需要的: 二.学习入门曲线陡峭,但是使用后期异常简单,效率超高: 三.号称可以跟上 ...

  9. LeetCode OJ-- Restore IP Addresses

    https://oj.leetcode.com/problems/restore-ip-addresses/ string到int的ip地址格式化. 分别用 i+1,j+1,k+1,表示前三个地址段的 ...

  10. 监听EditText输入完成

    最近有个需求,要在用户输入完快递单号之后,请求快递100接口,拿到快递公司信息.总不能用户输入一个数字就请求一次吧,给服务器造成不必要的压力(虽然不是自家服务器).但是又无法知晓用户何时输入完毕,每家 ...