• leetcode64:maximal-rectangle


    题目描述

    给出一个只包含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.
    class Solution {
    public:
        int maximalRectangle(vector<vector<char> > &matrix) {
            if (matrix.size()==0)
                return 0;
            int m=matrix.size();
            int n=matrix[0].size();
            vector< int> h(n);
            int maxS=0;
            int num;
            stack<int> st;
            st.push(-1);
            for (int i=0;i<m;i++)
            {
                for (int j=0;j<n;j++){
                    if (matrix[i][j]=='1')
                        h[j]++;
                    else
                        h[j]=0;
                    
                }
                
                for (int j=0;j<n;j++){
                    while (st.top()!=-1 && h[j] <h[st.top()])
                    {
                        num=st.top();
                        st.pop();
                        maxS=max(maxS,(j-1-st.top())*h[num]);
                        
                    }
                    st.push(j);
                }
                while (st.top()!=-1){
                    num=st.top();
                    st.pop();
                    maxS=max(maxS,(n-1-st.top())*h[num]);
                }
            }
            return maxS;
        }
    };
  • 相关阅读:
    原创的java数据访问框架
    在ASP.NET中使用Session常见问题集锦
    Infragistics中WebGrid的MultiColumn Headers设计
    常用asp.net代码
    如何实现函数IF的嵌套超过七层?
    Microsoft® Visual Studio® 2005 Team Suite Service Pack 1
    ASP.NET中常用的文件上传下载方法
    ASP.NET 2.0:使用用户控件和定制的Web部件个人化你的门户网站
    office2007TW
    http://www.ydowns.com/download/53279.rar
  • 原文地址:https://www.cnblogs.com/hrnn/p/13428018.html
Copyright © 2020-2023  润新知