。。。剑指Offer之——用两个栈实现队列。。。
生活随笔
收集整理的這篇文章主要介紹了
。。。剑指Offer之——用两个栈实现队列。。。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 // stack1用來表示入棧
2 Stack<Integer> stack1 = new Stack<Integer>();
3 // stack2用來表示出棧
4 Stack<Integer> stack2 = new Stack<Integer>();
5
6 // 隊列的入隊,就用stack1進行入棧
7 public void push(int node) {
8 stack1.push(node);
9 }
10
11 public int pop() {
12 // 隊列出隊的時候,如果stack2為空,就把stack1里面所有的元素加入到stack2中
13 if(stack2.isEmpty()){
14 while (!stack1.isEmpty()){
15 stack2.push(stack1.pop());
16 }
17 }
18 // 隊列出隊的時候,如果stack2不為空,直接返回stack2的棧頂元素
19 return stack2.pop();
20 }
?
轉載于:https://www.cnblogs.com/yingmeng/p/10769308.html
總結
以上是生活随笔為你收集整理的。。。剑指Offer之——用两个栈实现队列。。。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xp怎么显示隐藏分区 XP如何显示和隐藏
- 下一篇: 实战ELK(9) Elasticsear