题目
Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
思路
两个栈模拟队列
代码
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Queue {
public:
void push(int x) {
firStack.push(x);
}
void pop(void) {
if(!secStack.empty()){
secStack.pop();
return;
}
if(firStack.empty()){
return;
}
while(!firStack.empty()){
secStack.push(firStack.top());
firStack.pop();
}
secStack.pop();
}
int peek(void) {
if(!secStack.empty()){
return secStack.top();
}
if(firStack.empty()){
return -1;
}
while(!firStack.empty()){
secStack.push(firStack.top());
firStack.pop();
}
return secStack.top();
}
bool empty(void) {
return firStack.empty() && secStack.empty();
}
private:
stack<int> firStack;
stack<int> secStack;
};
int main(){
Queue q;
for(int i = 0;i < 5;++i){
q.push(i);
}
while(!q.empty()){
cout<<q.peek()<<" ";
q.pop();
}
q.push(8);
cout<<q.peek()<<endl;
q.pop();
cout<<q.peek()<<endl;
return 0;
}
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
232.Implement_Queue_using_Stacks用栈实现队列【LeetCode单题讲解系列】
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
看leetcode吃力资料结构与演算法学习笔记 Week1 中秋节放假 Week2 说明上课方式与计分规则 Week3 完成LeetCode 707. Design Linked List Week4 完成LeetCode 155. Min Stack 完成LeetCode 232. Implement Queue ...
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
Leetcode 题解.pdf
LeetCode 后端.zip
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
Recording personal Java, Python, JavaScript solutions for Leetcode problems. 记录个人 Java, Python, JavaScript 的Leetcode题解.zip
Leetcode101.zip
刷leetcode总结.md
原创:leetcode 111. 二叉树的最小深度记住:最小深度和最大深度方法不同。* Definition for a binary tree node.in
原创:leetcode 107. 二叉树的层次遍历 II【队列】* Definition for a binary tree node.
原创:leetcode 5. 最长回文子串//寻找以i-1,i为中点偶数长度的回文//寻找以i为中心的奇数长度的回文。
225.Implement_stack_using_queues用队列实现栈【LeetCode单题讲解系列】
My Solutions to Leetcode problems. All solutions support C
原创:leetcode 22.括号生成【回溯】对待这种问题,千万别暴力搜索,那样太笨了。但是这个方法是最容易理解的//回溯法 (后面的括号) 不可以大于 (前面
LeetCode674. 最长连续递增序列674. 最长连续递增序列解题思路:记录每次递增序列的长度,max存储最大长度// 递增序列更新最大长度} else