//方案1

import java.util.Stack;

public class Solution {

Stack<Integer> stack1 = new Stack<Integer>();

Stack<Integer> stack2 = new Stack<Integer>();

public void push(int node) {
    if(stack1.isEmpty())
        stack1.push(node);
    else{
        while(!stack1.isEmpty())
            stack2.push(stack1.pop());

        stack1.push(node);

        while(!stack2.isEmpty())
            stack1.push(stack2.pop());
    }

}

public int pop() {

    return stack1.pop();
}

}

//方案二

import java.util.Stack;

public class Solution {

Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();

public void push(int node) {
    while(!stack2.isEmpty())
        stack1.push(stack2.pop());
    stack1.push(node);
}

public int pop() {

    while(!stack1.isEmpty())
        stack2.push(stack1.pop());

    return stack2.pop();
}

}

方案三:

import java.util.Stack;

public class Solution {

Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();

public void push(int node) {
    stack1.push(node);
}

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

    return stack2.pop();

}

}

三种方案对比情况下:

个人认为应该选第三种,原因:因为如果是进队列,允许两个栈都有元素,出队列时,允许两个栈有元素。

11-30 06:42