09-用两个栈实现队列

2020-05-05  本文已影响0人  一方乌鸦
class CQueue {
  Stack<Integer> s1;
  Stack<Integer> s2;
  public CQueue() {
    s1 = new Stack<>();
    s2 = new Stack<>();
  }
  
  public void appendTail(int value) {
    s1.push(value);
  }
  
  public int deleteHead() {
    if (s2.isEmpty()) {
      if (s1.isEmpty()) return -1;
      while(!s1.isEmpty()) {
        s2.push(s1.pop());
      }
    }
    return s2.pop();
  }
}

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */
上一篇 下一篇

猜你喜欢

热点阅读