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. appium+python自动化49-yaml管理定位元素

    前言 如何高效管理定位元素,这个是很有学问的问题,也是面试必问的[以下纯属个人观点,勿喷!]. 有的人用xml管理页面定位元素,这种逼格略高,但是小编认为学习成本大,贼麻烦. 有的人提到用excel管 ...

  2. activiti源码笔记对标的博客

    http://blog.csdn.net/bluejoe2000/article/category/2812511/2 白乔专栏 http://blog.csdn.net/bluejoe2000/ar ...

  3. text-align 属性,输入框数字向右靠

    1.业务需求:金额输入框数字向右靠 2.HTML文件 <td id="otherPay_Td"> <input id="otherPay" t ...

  4. JS,jquery动态向已经存在的css文件中添加class样式

    $("h1,h2,p").addClass("blue") $(".class")addClass("class_name&quo ...

  5. java中各种数据类型占用字节数

    1字节(Byte)=8bit java的基本类型 类型 所占字节 byte 1 short 2 int 4 long 8 float 4 double 8 char 2 String中字母和汉字所占字 ...

  6. 三分钟教你学Git(十八) - 重写历史

    git filter-branch 同意你使用一个单一命令来大范围地更改历史.所以这个命令要慎用. 1假如你想对全部的commits删除一个文件. git filter-branch --tree-f ...

  7. (转)IntelliJ Idea 常用快捷键列表 for win

    Ctrl+Shift + Enter,语句完成 ctrl+alt+左键 进入实现方法 “!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shif ...

  8. html 接收GET请求参数

       function GetQueryString(name)   {        var reg = new RegExp("(^|&)"+ name +" ...

  9. VScode eslint-plugin-vue 自动修复eslint报错

    1.安装插件 npm i -g eslint-plugin-vue 2.修改项目跟路径下的文件:.eslintrc | .eslint.js 3.添加eslint 和 vetur 插件 4.修改vsc ...

  10. sql jion

    A Visual Explanation of SQL Joins I thought Ligaya Turmelle's post on SQL joins was a great primer f ...