Where is the problems in my code?(HELP)


  • 0
    C

    where is the problems: that's WA! but it's all right on my machine.

    class MinStack{
    public:
    stack<long> s;
    long min;
    void push(int x){
        if (s.empty()) {
            s.push(0);
            min = x;
        }else{
            s.push(x-min);
            if (x < min) {
                min = x;
            }
        }
    }
    
    void pop(){
        if (s.empty()) {
            return;
        }else{
            if (s.top() < 0) {
                min = min - s.top();
            }
            s.pop();
        }
    }
    
    int top(){
        if (s.empty()) {
            return NULL;
        }else{
            if (s.top() > 0) {
                return (int)(min+s.top());
            }else{
                return (int)min;
            }
        }
    }
    
    int getMin(){
        if (s.empty()) {
            return NULL;
        }else{
            return (int)min;
        }
    }
    

    };


  • 0
    L

    One thing unusual that I noticed is that you return NULL as a type "int" in both the top() and getMin() functions. The NULL might not be considered to be an integer type for the compiler of the Online Judge.


  • 0
    C

    No~ that's not right. you can test this code. the error is WA. but the stack is not empty in test case.


  • 0
    C

    How do you recover previous min while keep popping?


Log in to reply
 

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