40. Implement Queue by Two Stacks【medium】
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.
push(1)
pop() // return 1
push(2)
push(3)
top() // return 2
pop() // return 2
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】的更多相关文章
- 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. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 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 ...
随机推荐
- 【FTP】使用org.apache.commons.net.ftp.FTPClient 实现FTP的上传下载
在此之前,在项目中加上FTP的架包 第一步:配置FTP服务器的相关配置 FtpConfig.java 实体类(配置类) package com.sxd.ftp; public class FtpCo ...
- 网页中输出漂亮格式的Php数组神器
写网页的时候经常需要在页面中打印数组,但格式特别难看,看看一个html神器吧<pre>标签,能非常标准的显示数组格式 使用的时候只需要这样打印你的数组就OK了,太好用了,神器! 只需要两句 ...
- android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[问题已解决]
11-06 18:29:15.582: W/WebView(27807): java.lang.Throwable: A WebView method was called on thread 'Ja ...
- js调用高德地图的搜索api
var city = $('#city')[0].value; AMap.plugin(['AMap.Autocomplete'],function(){ var autoOptions = { ci ...
- http://blog.csdn.net/xingfuzhijianxia/article/details/6433918
http://blog.csdn.net/xingfuzhijianxia/article/details/6433918
- 《大话操作系统——做坚实的project实践派》(5)
有人说我不是仅仅会玩X86吗?我如今铁板钉钉的申明:我事实上兼通:ARM.IA-32.IA-32e.AMD64. 了解MIPS.但没做过MIPS由于确实没有对应的MIPS硬件平台.
- 加加减减(你真的懂++--吗) C#
目录 TOC \o "1-3" \h \z \u 自增量. PAGEREF _Toc456268662 \h 1 08D0C9EA79F9BACE118C8200AA004BA9 ...
- python中,== 与 is 之间区别
在python中,== 与 is 之间既有区别,又有联系,本文将通过实际代码的演示,力争能够帮助读到这篇文章的朋友以最短的时间理清二者的关系,并深刻理解它们在内存中的实现机制. 扯淡的话不多说,下面马 ...
- Android 微信支付,授权,分享回调区分记录
我们做项目中避免不了和微信打交道,其中最常用的也就是授权登录与分享和支付了. 本篇文章记录这三个功能同时使用的时候,回调怎么来区分.因为每个功能都有自己的回调状态.前期集成与发送,资料很多了就不在这里 ...
- js 数组去重方法汇总
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...