1. 程式人生 > 實用技巧 >$router和$route的區別

$router和$route的區別

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        if(heights.size()==0) return 0;

        stack<int>st;
        int res=-1;
        int len=heights.size();
        //遍歷陣列,將高度依次從小到大入棧(因為找最近的最小值)
        for( int i=0;i<len;i++ )
        {
            //如果棧不為空且入棧的數要比棧頂元素小或等於的話,就要出棧,一出棧就計算矩形的大小
            //矩形的長就是左邊界和右邊界的差值,左邊界就是pop以後的棧頂的下標對應,右邊界就是要入棧的下標
            //矩形的高就是彈出值下標對應的值
            while( !st.empty()&&heights[i]<=heights[st.top()] )
            {
                int h=heights[st.top()];
                st.pop();
                int l=st.empty() ? -1 : st.top();
                res= max( res,( i-l-1 )*h );
            }
            st.push( i );
        }
        while( !st.empty() )
        {
            int h=heights[st.top()];
            st.pop();
            int l=st.empty() ? -1: st.top();
            res= max( res,( len-l-1 )*h ); 
        }
        return res;
    }
};

  

class Solution {
public:
    //每一行直接套用陣列內求最大矩形
    int largestRectangleArea(vector<int> heights) {
        if(heights.size()==0) return 0;

        stack<int>st;
        int res=-1;
        int len=heights.size();
        //遍歷陣列,將高度依次從小到大入棧(因為找最近的最小值)
        for( int i=0;i<len;i++ )
        {
            //如果棧不為空且入棧的數要比棧頂元素小或等於的話,就要出棧,一出棧就計算矩形的大小
            //矩形的長就是左邊界和右邊界的差值,左邊界就是pop以後的棧頂的下標對應,右邊界就是要入棧的下標
            //矩形的高就是彈出值下標對應的值
            while( !st.empty()&&heights[i]<=heights[st.top()] )
            {
                int h=heights[st.top()];
                st.pop();
                int l=st.empty() ? -1 : st.top();
                res= max( res,( i-l-1 )*h );
            }
            st.push( i );
        }
        while( !st.empty() )
        {
            int h=heights[st.top()];
            st.pop();
            int l=st.empty() ? -1: st.top();
            res= max( res,( len-l-1 )*h ); 
        }
        return res;
    }
    
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.size()==0||matrix[0].size()==0) return 0;
        vector<int>height(matrix[0].size(),0);
        int res=-1;
        //算出每行的直方圖高度,如果這個單元格是1就讓上面一個單元格的數值加一,不然就是0
        for( int i=0;i<matrix.size();i++ )
        {
            
            for( int j=0;j<matrix[0].size();j++ )
            {
                height[j]= matrix[i][j]=='1'? height[j]+1 : 0;    
            }
            res=max( res,largestRectangleArea(height) );
        }

        return res;
    }

};