Why "void value not ignored as it ought to be"


  • 0
    R

    I admit that I am not an expert in C++. The following solution is modified from my java version. The same algorithm works in Java. So it must be some simple mistake in my C++.

    Thanks for your help. I have labeled the problematic line.

    class Solution {
    public:
        int largestRectangleArea(vector<int> &height) {
                   
            if (height.size()==0) return 0;
            
            stack<int> index;
            index.push(-1);
            int result=0;
            int top;
            
            for  (int i=0;i<height.size();i++){
                    //Start calculate the max value
                while (index.top()>-1){
                    if (height.at(index.top())>height.at(i)){
                        top=index.pop();  //This line always has problem                  
                        result=max(result,height.at(top)*(i-1-index.top()));  
                    }else break;
                }
    
                    
                index.push(i);
            }
            while(index.top()!=-1){
            	top=index.pop();
                result=max(result,height.at(top)*(height.size()-1-index.top()));
            }        
            return max;
        }
    };

  • 2
    S

    Break it down into two statements: top = index. top() and index. pop(). In C++, stack:: pop() only pops the top element but does not return it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.