As the title described, you should only use two stacks to implement a queue's actions.

The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.

Both pop and top methods should return the value of first element.

 
Example

push(1)
pop() // return 1
push(2)
push(3)
top() // return 2
pop() // return 2
Challenge

implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE.

解法一:

 class MyQueue {
public:
stack<int> stack1;
stack<int> stack2; MyQueue() {
} void push(int element) {
stack1.push(element);
} void adjust() {
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
}
} int pop() {
adjust();
int temp = stack2.top();
stack2.pop();
return temp;
} int top() {
adjust();
return stack2.top();
}
};

40. Implement Queue by Two Stacks【medium】的更多相关文章

  1. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

  2. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  3. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  4. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  5. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  6. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  7. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  8. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  9. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

随机推荐

  1. Netty游戏服务器之六服务端登录消息处理

    客户端unity3d已经把消息发送到netty服务器上了,那么ServerHandler类的public void channelRead(ChannelHandlerContext ctx, Obj ...

  2. Java9模块化(Jigsaw)初识

    Java9经历了多次跳票,终于要在9月份正式发布,原计划Jigsaw在Java7就有的,也终于在Java9里面提供了,简单总结下. 对比 Java9 以前 上面2个图分别对应的分别是JDK8/9的目录 ...

  3. AWR Report 关键参数详细分析

    WORKLOAD REPOSITORY report for DB Name DB Id Instance Inst num Startup Time Release RAC CALLDB 12510 ...

  4. Delphi中Frame的使用方法(1)

    Frame是组件面板上的第一个组件,但不是每个人都知道怎么用它,因为它不像Button和Label一样简单明了.实际上,Frame按钮只是打开一个Frame的列表,如果你没有创建任何的Frame,自然 ...

  5. unity 实时间接光照 解决方案

    https://www.youtube.com/watch?v=D7LjsabD4V4 这个很强 他runtime bake lightprobe 之后走assetbundle加载 Place Pro ...

  6. apache2.4 的安装

    Apache2.4 安装包下载地址 http://httpd.apache.org/docs/current/platform/windows.html#down 选择ApacheHaus 进入后 这 ...

  7. css border-sizing 用法与理解

    浏览器支持 IE Firefox Chrome Safari Opera  支持  支持  支持  支持  支持 Internet Explorer.Opera 以及 Chrome 支持 box-si ...

  8. 【云计算】Docker容器时间同步如何配置?

    示例: # 3.8 配置时区.时钟同步 # configure timezone & ntp RUN echo ${TIME_ZONE} > /etc/timezone \ && ...

  9. 开源 SHOPNC B2B2C结算营运版 wap IM客服 API 手机app 短信通知

    源码我们这里简单的测试了下,具体的请自行下载测试.这套源码官方售价很高,在这里完全免费分享,无任何限制,安装也简单. 源码下载后请自行检测安全,在使用过程中发生的任何问题请自行处理,本站不承担任何责任 ...

  10. linux mysql 更改MySQL数据库目录位置

    MySQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步: 1.home目录下建立data目录 cd /home mkdir data 2 ...