题目

image.png

解题代码

  1. class CQueue {
  2. Stack<Integer> stackPush ; //压栈
  3. Stack<Integer> stackPop ; //出栈
  4. public CQueue() {
  5. // 初始化
  6. stackPush = new Stack<>();
  7. stackPop = new Stack<>();
  8. }
  9. public void appendTail(int value) {
  10. stackPush.push(value); //入栈
  11. }
  12. public int deleteHead() {
  13. if(stackPop.isEmpty() ) {
  14. while( !stackPush.isEmpty() ) {
  15. //将入栈信息全部压入弹出栈
  16. stackPop.push(stackPush.pop() );
  17. }
  18. }
  19. return stackPop.isEmpty() ? -1 : stackPop.pop();
  20. }
  21. }
  22. /**
  23. * Your CQueue object will be instantiated and called as such:
  24. * CQueue obj = new CQueue();
  25. * obj.appendTail(value);
  26. * int param_2 = obj.deleteHead();
  27. */