Cracking coding interview(3.5)使用2个堆栈实现一个队列
来源:互联网 发布:iis配置php 编辑:程序博客网 时间:2024/04/28 13:13
3.5 Implement a MyQueue class which implements a queue using two stacks.
explanation:
1.MyQueue实现原理:当MyQueue需要offer(add)一个元素时,直接添加到堆栈s1中即可,若要poll(remove)一个元素时,则需要借助堆栈s2,将s1中元素pop并且push到s2中,此时s2栈顶元素即是对头元素。之后再将s2中元素pop并且push回s1中。
2.MyQueue2针对MyQueue的优化。堆栈s2中的元素不再回到s1.堆栈s1用于存放最新的push的元素,堆栈s2用于将堆栈s1中的现有元素反序,这样最先入队的几个元素都会在s2中。当MyQueue队列要弹出一个元素时,只需要从s2中弹出元素即可,若s2为空,则将s1中的最新的元素再次pop->push到s2。如此往复。
import java.util.Stack;class MyQueue{private Stack<Integer> s1 = new Stack<Integer>();private Stack<Integer> s2 = new Stack<Integer>();//time complexity:O(1), space complexity:O(1)public boolean offer(int val){s1.push(val);return true;}//time complexity:O(n) space complexity:O(n)public int poll(){if(empty())return Integer.MIN_VALUE;else{while(!s1.empty())s2.push(s1.pop());int val = s2.pop().intValue();while(!s2.empty())s1.push(s2.pop());return val;}}public boolean empty(){return s1.empty();}}//improvement for MyQueueclass MyQueue2{Stack<Integer> s1 = new Stack<Integer>();Stack<Integer> s2 = new Stack<Integer>();public boolean offer(int val){s1.push(val);return true;}//time complexity gradually reduced, most cases is O(1)public int poll(){if(s2.empty())while(!s1.empty())s2.push(s1.pop());if(s2.empty())return Integer.MIN_VALUE;elsereturn s2.pop().intValue();}public boolean empty(){if(s1.empty() && s2.empty())return true;elsereturn false;}}public class Solution{public static void main(String[] args){//test for MyQueueMyQueue mq = new MyQueue();int i=0;for(;i < 20;i++)mq.offer(i);while(--i > 10 && !mq.empty())System.out.print(mq.poll()+" ");System.out.println();for(i=40;i < 50;i++)mq.offer(i);while(!mq.empty())System.out.print(mq.poll()+" ");System.out.println();//test for MyQueue2MyQueue2 mq2 = new MyQueue2();for(i=100;i < 102;i++)mq2.offer(i);while(i-- != 101 &&!mq2.empty())System.out.print(mq2.poll()+" ");for(i=102;i < 120;i++)mq2.offer(i);while(!mq2.empty())System.out.print(mq2.poll()+" ");System.out.println();}}
0 0
- Cracking coding interview(3.5)使用2个堆栈实现一个队列
- Cracking coding interview(3.1)用一个数组实现3个堆栈
- Cracking coding interview(3.6)堆栈排序
- Cracking The Coding Interview 3.5
- Cracking coding interview(3.3)实现堆栈集合和popAt函数实现
- Cracking coding interview(3.2)堆栈实现常量复杂度的min函数
- Cracking the coding interview Q1.2
- Cracking the Coding Interview Chap1 Q1.2
- Cracking the coding interview--Q1.2
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q3.2
- Cracking the coding interview--Q1.2
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q3.2
- Cracking the coding interview--Q4.2
- Cracking the coding interview--Q5.2
- Cracking the coding interview--Q8.2
- Cracking the coding interview--Q9.2
- 如何将Vim打造成一个成熟的IDE
- 点评国内各大在线app生成平台
- hadoop2.2.0源码编译
- java I/O流
- 好用的PDF转换PPT软件推荐
- Cracking coding interview(3.5)使用2个堆栈实现一个队列
- BigDecimal.setScale()方法
- 25留数及其应用(一)
- C++ string 实现大整数相加减
- .NET技术+25台服务器怎样支撑世界第54大网站
- 如何找工作使用Java
- 如何在印度选择最好的移动游戏开发公司
- 第一个CUDA项目(LNK1123 failure during conversion to coff file invalid or corrupt)
- JS封装类似JAVA中的MAP