WHY THERE IS A RUN TIME ERROR ON MY C++ SOLUTION


  • 0
    S
    class MinStack {
    
    public:
        stack<int> st;
        stack<int> st2;
        vector<int> count;
        int min;
        void push(int x) {
            if(st.size()==0)
                {
                    min = x;
                    st2.push(x);
                    count.push_back(1);
                }
                
            else
                {
                    if(x==min){
                        (*(count.end()-1))++;
                    }
                    else if(x < min)
                    {
                        count.push_back(1);
                        st2.push(x);
                        min = x;
                    }
                }
            st.push(x);
        }
    
        void pop() {
            if(st.top()==min)
            {
               (*(count.end()-1))--;
                if(*(count.end()-1)==0)
                {
                    count.erase(count.end()-1);
                    st2.pop();
                    min=st2.top();
                }
            }
            st.pop();
        }
    
        int top() {
            return st.top();
        }
    
        int getMin() {
            return min;
        };
    

    The error information looks like that:

    Last executed input: push(2147483646),push(2147483646),push(2147483647),top,pop,getMin,pop,getMin,pop,push(2147483647),top,getMin,push(-2147483648),top,getMin,pop,getMin

    Is this a bug of the system?


  • 0
    H

    I have the same problem and still don't know how to fix it.


  • 0
    S

    I have tried this code on visual studio 2013. However, the error is not about my code. I just write "int a = -2147483648;" and there will be an error.


  • 0
    H

    My runtime error is very strange. It gave me a quite long sequence but told nothing more about extra error message. The sequence is given as following:
    push(-124),push(-164),getMin,getMin,getMin,pop,getMin,push(-24),push(-100),getMin,push(33),top,push(-22),getMin,push(197),getMin,push(127),push(122),getMin,push(139),push(142),push(189),getMin,top,push(-40),getMin,getMin,getMin,pop,push(174),getMin,getMin,getMin,top,top,push(-28),top,getMin,push(177),push(-117),getMin,top,push(42),getMin,getMin,top,top,push(-43),getMin,getMin,pop,pop,top,getMin,push(158),pop,push(25),push(-197),getMin,push(-120),push(-74),pop,getMin,getMin,push(51),push(60),getMin,push(182),getMin,push(-161),push(57),push(156),push(-182),push(28),getMin,pop,getMin,push(122),push(-119),push(-101),push(77),pop,getMin,top,getMin,getMin,top,push(-185),pop,push(-25),top,push(-7),push(91),push(-79),getMin,top,push(-168),push(115),push(-14),top,push(-95),push(-24),push(34),top,getMin,push(20),getMin,push(-88),top,push(1),push(186),getMin,pop,pop,push(-45),pop,getMin,top,getMin,top,push(89),getMin,push(-65),getMin,getMin,push(-100),getMin,push(-85),top,getMin,getMin,push(91),getMin,pop,getMin,getMin,getMin,top,pop,push(-80),top,getMin,getMin,getMin,push(-66),getMin,push(-3),pop,push(-185),push(-15),push(-185),push(-8),pop,getMin,pop,pop,push(177),top,push(-160),pop,getMin,getMin,push(-39),push(-67),getMin,pop,push(187),top,push(178),push(32),top,push(-119),push(-83),push(162),getMin,push(124),pop,getMin,push(-86),push(-21),getMin,push(19),getMin,pop,getMin,push(-126),push(-110),top,top,push(71),getMin,pop,push(-64),getMin,push(-21),push(148),getMin,top,pop,push(72)


  • 0
    S

    I've solved my problem with the following expression

    st2.size()?min = int(st2.top()):min=INT_MAX;

    I've not considered how to deal with the value"min" if the stack is poped to empty before. I hope it can help you!


Log in to reply
 

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