what's the difference between these two code?


  • 0
    B

    For the pop function, I have these two versions. I think both should work, but the fact is the second cannot pass the test case. Can anyone point me what's wrong with the second? Thanks!

    public void pop() {
        if (stack.isEmpty()) {
            return; 
        }
        int val = stack.pop();
        if (val == minStack.peek()) {
            minStack.pop();
        }
    }
    public void pop2() {
       if (stack.isEmpty()) {
         return;
       }
       if (stack.pop() == minStack.peek()) {
             minStack.pop();
       }
    }

Log in to reply
 

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