todorex / Coding-Interviews

📚剑指Offer(java版)
458 stars 130 forks source link

用栈实现队列的代码中有个Bug #3

Closed LiYinJiang closed 4 years ago

LiYinJiang commented 4 years ago

public int pop() throws Exception { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } }

    if (stack1.isEmpty()) {
        throw new Exception("queue is empty");

    }
    return stack2.pop();

}

百分百抛Bug

todorex commented 4 years ago

@LiYinJiang 确实,应该是

 if (stack2.isEmpty()) {
        throw new Exception("queue is empty");
 }

你提个pr吧

todorex commented 4 years ago

pr修复