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. hibernate缓存详解

    hibernate中提供了两级缓存,一级缓存是Session级别的缓存,它属于事务范围的缓存,该级缓存由hibernate管理,应用程序无需干预:二级缓存是SessionFactory级别的缓存,该级 ...

  2. 利用MVC模式简单设计jsp分页效果

    利用Mysql创建一个表Car 用Eclipse创建一个Dynamic Web Project 在lib目录下导入Mysql的jar包 创建如下文件 package com.bean; public ...

  3. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法

    代码改变世界 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pre ...

  4. Welcome-to-Swift-23访问控制(Access Control)

    访问控制可以限定你在源文件或模块中访问代码的级别,也就是说可以控制哪些代码你可以访问,哪些代码你不能访问.这个特性可以让我们隐藏功能实现的一些细节,并且可以明确的指定我们提供给其他人的接口中哪些部分是 ...

  5. POJ——1321棋盘问题(DFS+回溯)

    棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33272 Accepted: 16456 Description 在一 ...

  6. java面试题之synchronized和lock有什么区别

    synchronized和lock的区别: 类别 synchronized lock 存在层次 java的关键字,在jvm层面上 是一个类 锁的释放 1.以获取锁的线程执行完同步代码,释放锁 2.线程 ...

  7. 重置css样式

    如果有第三方插件或者想要覆盖css的样式的话,给他的样式设置auto就好了

  8. VMware 11 安装Mac 10.9

    VMware Workstation 11, 10, 9 and 8 Unlocker to Run Mac OS X Guests in Windows 8.1 and 7 VM10装Mac OS ...

  9. Dumb Bones(uva 10529)

    题意:给定n,表示要放n个骨牌,每次放下骨牌,有可能向左倒的概率为pl,向右倒的概率为pr,如果倒下,会将那一侧的骨牌全部推倒,可以选择位置先后放骨牌,问说一种放骨牌次数最少的期望是多少. /* 设d ...

  10. Beyond compare vs kdiff3

    這裡使用的 kdiff3 版本是 0.9.98   基於以下 三點,最終選擇了 beyond compare   1. kdiff3 不能刪檔案,     以下為例,不能刪1   2. kdiff3 ...