求在0-1矩阵中找出面积最大的全1矩阵


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

首先,想使用遍历两次的暴力方法解决是不靠谱的,先打消这个念头。

Largest Rectangle in Histogram0,那么当前一唯矩阵置为0,否则加1


class Solution {public:  int largestRectangleArea(vector<int> &h) {           stack<int> S;           h.push_back(0);           int sum = 0;           for (int i = 0; i < h.size(); i++) {                if (S.empty() || h[i] > h[S.top()]) S.push(i);                else {                     int tmp = S.top();                     S.pop();                     sum = max(sum, h[tmp]*(S.empty()? i : i-S.top()-1));                     i--;                }           }           return sum;      }  int maximalRectangle(vector<vector<char> > &matrix) {        if(matrix.size() == 0 || matrix[0].size() == 0)            return 0;        int maxArea = 0;        vector<int> temp(matrix[0].size(), 0);//用来调用辅助函数的参数向量        for(int i = 0; i < matrix.size(); i++){            for(int j = 0; j < matrix[0].size(); j++){ //在每一行都重置辅助向量                if(matrix[i][j] == '1')                    temp[j]++;                else                    temp[j] = 0; //断开了,就置零            }            int ret = largestRectangleArea(temp);            maxArea = ret > maxArea ? ret : maxArea;         }        return maxArea;   }  };