您的位置:首页 > 职场人生

面试题7:用两个栈实现队列

2017-06-15 22:21 169 查看
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();

    }
}

if是只要条件满足就执行一次,while是只要条件满足就不停地执行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: