Why "runtime error" when "push(-2147483648)"? And my AC answer


  • 1
    G
    class MinStack {
            std::stack<std::pair<int,int>> p;
            int min_ele;
    public:
    	void push(int x)
    	{
    		if (p.empty())
    		{
    			std::pair<int, int> e = std::make_pair(x, x);
    			min_ele = x;
    			p.push(e);
    			return;
    		}
    		x <= min_ele  ?  min_ele = x : min_ele = p.top().second;
    		std::pair<int, int> e = std::make_pair(x,min_ele);
    		p.push(e);
    	}
    	void pop()
    	{
    		p.pop();
    		min_ele = p.top().second;
    	}
    	int top()
    	{
    		return p.top().first;
    	}
    	int getMin()
    	{
    		return p.top().second;
    	}
    };

  • 1
    G

    I forget a line.

    void pop()
    {
        p.pop();
        if (!p.empty())
        min_ele = p.top().second;
    }

Log in to reply
 

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