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 ...
随机推荐
- ASP.NET Core管道深度剖析
ASP.NET管道 以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET运行时尚未加载).IIS 6引入了应用程序池的概念,一个工作 ...
- SpringMVC中拦截器的使用
什么是拦截器 拦截器通常一般指的是通过拦截从浏览器发往服务器的一些请求来完成某些功能的一段程序代码一般在一个请求发生之前,发生时,发生后我们都可以对请求进行拦截 拦截器可以做什么 拦截器可以用于权限验 ...
- es6数组去重复
var arr=[1,1,2,3,5,7,7,7] arr=Array.from(new Set(arr))
- hibernate反向工程 (eclipse和myeclipse)【转】
myeclipse下hibernate反向工程: 1.选择myeclipse hibernate视图 2.建立与后台数据库的连接 1).configure database driver: 2).添加 ...
- Javascript时间字符串比较
//startdate和enddate的格式为:yyyy-MM-dd hh:mm:ss //当date1在date2之前时,返回1;当date1在date2之后时,返回-1:相等时,返回0 funct ...
- string c++ 转义序列
std::string shaderVS = "\struct PSInput \{ \float4 position : SV_POSITION;\float4 color : COLOR ...
- linux(虚拟机中)与windows共享文件两种方法
Windows 下用 SourceInsight 与 Linux 协作编码 习惯了用SourceInsight 读写代码,在Linux下一时没找到类似的工具,vi的操作也不熟,偶尔看看或小 ...
- ExpandListView onChildClickListener 失效
http://stackoverflow.com/questions/11529472/expandablelistview-onchildclicklistener-not-work 首先声明: ...
- vim 命令的使用
稍微再研究一下vim的命令使用. ----------------------------------------------------------------------------------- ...
- Helpers.parallel_bulk in Python not working?
Helpers.parallel_bulk in Python not working? 学习了:https://discuss.elastic.co/t/helpers-parallel-bulk- ...