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的矩阵,求所有为1组成的最大矩阵的面积。

此题能够巧妙转化为求最大直方图面积的问题。

public class Solution {
//其思想是将每一列的1逐行相加,遇0为0。遇1相加
//然后转化为求每一行的最大直方图面积的求解
//最后求出最大全为1的矩形面积
public int maximalRectangle(char[][] matrix) {
//边界条件
if(matrix.length == 0 || matrix[0].length == 0){
return 0;
} /**
* 按列将每列的1逐行相加
*/
for(int j = 0; j < matrix[0].length; j++){
for(int i = 1; i < matrix.length; i++){
if(matrix[i][j] != '0'){//不是0才相加。是0无论
matrix[i][j] = (char) (matrix[i-1][j] + 1);
}
}
}
int maxArea = 0;//最大矩形面积
for(int i= 0; i < matrix.length; i++){
maxArea = max(matrix[i],maxArea);//循环求最大
}
return maxArea;
} /**
* 依据每行。求最大直方图的面积
* @param height char数组
* @param maxArea 当前最大面积
* @return
*/
private int max(char[] height,int maxArea){
if(height.length == 0){//为空直接返回
return maxArea;
}
/**
* 两个栈,分别存在高度和索引
*/
Stack<Character> stHeight = new Stack<Character>();
Stack<Integer> stIndex = new Stack<Integer>();
/**
* 遍历
*/
for(int i = 0 ; i < height.length; i++){
//栈为空。或者高度比栈顶高度大,入栈
if(stHeight.isEmpty() || height[i] > stHeight.peek()){
stHeight.push(height[i]);
stIndex.push(i);
}else if(height[i] < stHeight.peek()){//高度比栈顶高度小
int lastIndex = 0;//最后的索引值
while(!(stHeight.isEmpty()) && height[i] < stHeight.peek()){
lastIndex = stIndex.pop();
int area = (stHeight.pop() - '0')*(i - lastIndex);//计算面积
maxArea = maxArea < area ? area:maxArea;
}
stHeight.push(height[i]);//当前值入栈
stIndex.push(lastIndex);//最小索引入栈
}
}
//假设栈不为空。继续计算
while(!(stHeight.isEmpty())){
int area = (stHeight.pop() - '0')*(height.length - stIndex.pop());
maxArea = maxArea < area ? area:maxArea;
}
return maxArea;
}
}

详细代码和思路例如以下:

leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法的更多相关文章

  1. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

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

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

  3. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

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

  5. leetcode[85] Maximal Rectangle

    给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...

  6. leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  9. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

随机推荐

  1. GoldenGate 进程

    GoldenGate进程 Manager进程 Manager进程是GoldenGate的控制进程,运行在源端和目标端上.它主要作用有以下几个方面:启动.监控.重启Goldengate的其他进程,报告错 ...

  2. vi-vim和linux常用快捷键

    移动光标 上      k 下      j 左     h 右     l 移动光标到当前行行尾首 ^ 移动光标到当前行行尾  $ 移动到文件的第一行  gg 移动到文件的最后一行  G 移动到第1 ...

  3. PHP DES-ECB加密对接Java解密

    最近公司有个业务,需要对接第三方接口,但是参数是需要加密的,对方也只提供了一个java的demo,在网上到处搜索,没有找到直接就能用的方法,后来还是跟公司的Android工程师对接出来的,在这里记录一 ...

  4. 紫书 习题 10-3 UVa 1643(计算几何 叉乘)

    直观感觉对角线重合的时候面积最大 然后可以根据方程和割补算出阴影部分的面积 注意知道两点坐标,可以求出与原点形成的三角形的面积 用叉乘,叉乘的几何意义以这两个向量为边的平行四边形的面积 所以用叉乘除以 ...

  5. 内存,寄存器和cache的区别与联系

    1. 寄存器是中央处理器内的组成部份.寄存器是有限存贮容量的高速存贮部件,它们可用来暂存指令.数据和位址.在中央处理器的控制部件中,包含的寄存器有指令寄存器(IR)和程序计数器(PC).在中央处理器的 ...

  6. 【转】 Java 进行 RSA 加解密时不得不考虑到的那些事儿

    [转] Java 进行 RSA 加解密时不得不考虑到的那些事儿 1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适 公钥加密,私钥解密.加密的系统和解密的系统分开部署,加密的系统不应该同时 ...

  7. 第6章8节《MonkeyRunner源代码剖析》Monkey原理分析-事件源-事件源概览-小结

    本章我们重点环绕处理网络过来的命令的MonkeySourceNetwork这个事件源来阐述学习Monkey是怎样处理MonkeyRunner过来的命令的.以下总结下MonkeyRunner从启动Mon ...

  8. [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

    Rather than using Components to push streams into other Components, mapPropsStream allows you to cre ...

  9. nyoj 1104 just for you

    just for you 时间限制:1000 ms  |  内存限制:65535 KB 难度:0 描写叙述 今天tlp和ly想去看电影了到了电影院才发现买票的人特别多 .ly不想让tlp等着急了,就先 ...

  10. Android——4.2.2 文件系统文件夹分析

    近期公司要整android内部培训,分配给我写个培训文档.这里记录例如以下: 撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/details/4089 ...