阅读背景:

剑指offer-面试题9-用两个栈实现队列&用两个队列实现栈

来源:互联网 

一:用两个栈实现队列

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(){
        //如果stack2不为空,直接弹出栈顶元素,如果stack2为空,先将stack1元素推入stack2,再弹出
        if(stack2.isEmpty()){
            if(stack1.isEmpty())
                return -1;
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }else{
            return stack2.pop();
        }
    }
}import java.util.Stack;

publi



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: