讨论/技术交流/求助|希望大神帮忙看看,题目最小栈,本地运行没问题,但是leetcode上有问题//
求助|希望大神帮忙看看,题目最小栈,本地运行没问题,但是leetcode上有问题/
class MinStack {
public:
    stack<int> stack1;
    int min_num;
    /** initialize your data structure here. */
    MinStack() {
        int min_num = 99999999;
    }
    void push(int x) {
        if (x<=min_num) {
            stack1.push(min_num);
            min_num  = x;
        }
        stack1.push(x);
        
    }
    void pop() {
        int pre = stack1.top();
        stack1.pop();
        if (pre == min_num) {
            min_num = stack1.top();stack1.pop();
        }
        
        
    }
    int top() {
        return stack1.top();
    }
    int min() {
        return min_num;
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->min();
 */

不知道是什么原因~~

共 4 个回复

嗯嗯 确实,是我疏忽了 谢谢哟

嗯嗯 我懂了谢谢你~~

this.min_num = Integer.MAX_VALUE;试试

构造函数是不是重复定义了min_num