用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。...
生活随笔
收集整理的這篇文章主要介紹了
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素為int類型。 import java.util.Stack; public class Solution { ????Stack<Integer> stack1 = new Stack<Integer>(); ????Stack<Integer> stack2 = new Stack<Integer>(); ????public void push(int node) { ????????int temp = 0; ????????if (stack1.isEmpty()) { ????????????stack1.push(node); ????????????return; ????????} ????????while(!stack1.isEmpty()){ ????????????temp=stack1.pop(); ????????????stack2.push(temp); ????????} ????????stack1.push(node); ????????while(!stack2.isEmpty()){ ????????????temp = stack2.pop(); ????????????stack1.push(temp); ????????} ??????? ????} ????public int pop() { ???????if(stack1.isEmpty()){ ?????????????throw new RuntimeException(""); ???????} ????????return stack1.pop(); ????} }轉載于:https://www.cnblogs.com/bb3q/p/5084058.html
總結
以上是生活随笔為你收集整理的用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net平台是什么?.net平台的组成,
- 下一篇: Unity3d Fast Indirec