/*
给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
示例:
输入:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
输出: 6
*/
思想和leetcode:84. 柱状图中最大的矩形(java)差不多吧,84题是一个一维数组也就是一个x轴,在这里我把每一行看为一个x轴也就是说如实例是四个一维数组组成,首先先看一行也就是一维数组,一行中它也是宽和高,宽就不用说了,高就是他所在的这一列向上连续的个数,所以这就是二维转化一维的关键,也就是你需要先求出每一行中每一个的它向上连续的长度也就是所谓的高最后然后一行一行的算,比较出最大值就可以了
大致图意:
1,
["1","0","1","0","0"]
2,
["1","0","1","0","0"]
["1","0","1","1","1"]
["2","0","2","1","1"]
3,
["1","0","1","0","0"]
["1","0","1","1","1"]
["1","1","1","1","1"]
["3","1","3","2","2"]
4,
["1","0","0","0","0"]
["1","0","0","1","0"]
["1","0","0","1","0"]
["1","0","0","1","0"]
["4","0","0","3","0"]
84. 柱状图中最大的矩形(java)地址:https://blog.youkuaiyun.com/microopithecus/article/details/88745859
public class MaximalRectangle {
public int maximalRectangle(char[][] matrix) {
int res = 0;
if(matrix==null||matrix.length==0||matrix[0].length==0){
return res;
}
//用于存放最当前的高
int[][] matrixint = new int[matrix.length][matrix[0].length];
//第一层的高度是当前的数字最高为1
for (int i = 0; i < matrix[0].length; i++) {
matrixint[0][i] = matrix[0][i] - 48;
}
//循环所有一行一行的向下来计算每行的高度(一行一行的截取矩形的高度)
for (int i = 1; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
//如果为零说明断开不能与上一层相连
if (matrix[i][j] == '0') {
matrixint[i][j] = 0;
} else {
//不为零则进行计算加上本身的1
matrixint[i][j] = matrixint[i - 1][j] + 1;
}
}
}
// 每一行都可以看做是一个底 一行一行的进行算面积(知道高了找到相应最大的宽就好了)
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
//大于零的高有意义
if (matrixint[i][j] > 0) {
//定义两个指针进行搜索宽
int left = j - 1;
int right = j + 1;
int wide = 1;
//搜索每一个高比当前中间这个高的值(向左可以组成矩形)
while (left >= 0 && matrixint[i][left] >= matrixint[i][j]) {
left--;
wide++;
}
while (right < matrixint[0].length && matrixint[i][right] >= matrixint[i][j]) {
right++;
wide++;
}
//计算面积
int air = matrixint[i][j] * wide;
if (air > res) {
res = air;
}
}
}
}
return res;
}
public static void main(String[] args) {
MaximalRectangle m = new MaximalRectangle();
char[][] a = {
{'1', '0', '1', '0', '0'},
{'1', '0', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'1', '0', '0', '1', '0'}
};
System.out.println( m.maximalRectangle(a));
}