当前位置:网站首页>经典双栈实现队列,注意遍历栈的判定条件修改。
经典双栈实现队列,注意遍历栈的判定条件修改。
2022-07-28 21:51:00 【dor.yang】
题目内容
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
提示:
1 <= x <= 9
最多调用 100 次 push、pop、peek 和 empty
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/implement-queue-using-stacks
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
栈是前进后出,两个栈自然可以实现先进先出,我们只要输入在一个栈,输出在另一个栈,只有在输出栈空了的情况下,我们将输入栈的所有元素一并送到输出栈,就可以实现队列的先进先出功能了。不过这里要注意的是一定要把所有输入栈入输出栈,不然就什么都不是了。
具体实现的话确实是很简单,有一个小点注意一下就行,就是栈的遍历是不能再循环判定中用a.size()这样的格式的,因为这个变量在循环中可变,容易走不完。
代码
class MyQueue {
public:
stack<int> a;
stack<int> b;
MyQueue() {
}
void push(int x) {
a.push(x);
}
int pop() {
if(b.size()==0){
int l=a.size();
for(int i=0;i<l;i++){
int num=a.top();
a.pop();
b.push(num);
}
}
int ans=b.top();
b.pop();
if(b.size()==0){
int l=a.size();
for(int i=0;i<l;i++){
int num=a.top();
a.pop();
b.push(num);
}
}
return ans;
}
int peek() {
if(b.size()==0){
int l=a.size();
for(int i=0;i<l;i++){
int num=a.top();cout<<num<<endl;
a.pop();
b.push(num);
}
}
return b.top();
}
bool empty() {
if(b.size()==0&&a.size()==0){
return true;
}
else{
return false;
}
}
};
/** * Your MyQueue object will be instantiated and called as such: * MyQueue* obj = new MyQueue(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->peek(); * bool param_4 = obj->empty(); */
边栏推荐
- Object object
- 英特尔数据中心GPU正式发货,以开放灵活提供强劲算力
- 事件抽取文献整理(2008-2017)
- mongodb索引添加、查看、导出、删除
- Byte 8 years' experience of female test Director - for girls who want to change careers or are about to enter the testing industry
- 2022T电梯修理考试试题及模拟考试
- Why did "you" become a test / development programmer? The value of your existence
- Array array object
- Arduino uno driver universe 1.8 'TFT SPI screen example demonstration (including data package)
- 阻塞式队列
猜你喜欢
随机推荐
【自】-刷题-BFS
金仓数据库 KingbaseES 与 Oracle 的兼容性说明(3. 常用函数)
酒店预订系统数据库房间库存设计思路
How to add the index of a set in mongodb to another set in mongodb
22 Niuke multi school Day1 I - Introduction to chiitoitsu DP
【自】-刷题-峰值
【自】-刷题-字符串
如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
解决线程安全问题&&单例模式
My second uncle is angry and swipes the screen all over the network. How can he cure my spiritual internal friction?
Swift type attribute and its attentions
High quality subroutine 3 - a good name
Neglected smart TV applet field
22牛客多校day1 J - Serval and Essay 启发式合并
如何开一家盈利的健身房?我用1年回本的经验告诉你,别谈恋爱
Huawei wireless device configuration uses WDS technology to deploy WLAN services
Development of small programs ①
Few people can really play in the "aftermarket" of the whole house intelligent fire collection
刨根问底学 二叉树
机器学习问题笔记









