一、题目内容

image.png

二、题解

解法1:

思路

代码

  1. public class Solution {
  2. Stack<Integer> stack1 = new Stack<Integer>();
  3. Stack<Integer> stack2 = new Stack<Integer>();
  4. public void push(int node) {
  5. stack1.push(node);
  6. }
  7. public int pop() {
  8. if (stack2.isEmpty()) {
  9. while (!stack1.isEmpty()) {
  10. stack2.push(stack1.pop());
  11. }
  12. }
  13. if (!stack2.isEmpty()) {
  14. return stack2.pop();
  15. } else {
  16. return -1;
  17. }
  18. }
  19. }