My code is correct in eclipse. But it is wrong when i put it to OJ


  • 0
    Z
    class MinStack {
    public static int CAPACITY = 10;
    public static int CUR = 0;
    int [] a = new int[CAPACITY];
    int [] min = new int[CAPACITY];
    
    public void push(int x) {
        if(CUR + 1 > CAPACITY){
            CAPACITY *= 2;
            int [] b = new int[CAPACITY]; 
            int [] bMin = new int[CAPACITY];
            for(int i=0;i<CUR;i++){
                b[i] = a[i];
                bMin[i] = min[i];
            } 
            a = b;
            min = bMin;
        }
        a[CUR] = x;
        if(CUR==0){
            min[CUR] = x;
        }else{
            min[CUR] = Math.min(x,min[CUR-1]);
        }
        CUR++;
    }
    
    public void pop() {
        CUR--;
    }
    
    public int top() {
        return a[CUR-1];
    }
    
    public int getMin() {
        return min[CUR-1];
    }
    

    }

    case:
    Input: push(2),push(0),push(3),push(0),getMin,pop,getMin,pop,getMin,pop,getMin

    Output: [0,0,0,0]

    Expected: [0,0,0,2]


  • 0
    R

    public static int CAPACITY = 10;
    public static int CUR = 0;

    Are you sure you want to declare them as static?


  • 0
    Z

    It is correct after deleting 'static'. THX.


Log in to reply
 

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