Implement Stack using Queues leetcode
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
不用多想,可以使用两个queue来实现,代码如下
耗时在pop上
class Stack {
public:
// Push element x onto stack.
void push(int x) {
que1.push(x);
}
// Removes the element on top of the stack.
void pop() {
while (que1.size() > )
{
que2.push(que1.front());
que1.pop();
}
que1.pop();
while (!que2.empty())
{
que1.push(que2.front());
que2.pop();
}
}
// Get the top element.
int top() {
return que1.back();
}
// Return whether the stack is empty.
bool empty() {
return que1.empty();
}
private:
queue<int> que1;
queue<int> que2;
};
查看其它人的代码,发现居然可以使用一个queue来实现,实现思路非常巧妙,每一次push操作都将queue之前的元素都移到后面,使其保持栈的排序
如:
push 1
1
push 2
2 1
push 3
3 2 1
class Stack {
public:
queue<int> que;
// Push element x onto stack.
void push(int x) {
que.push(x);
for (int i = ; i<que.size() - ; ++i) {
que.push(que.front());
que.pop();
}
}
// Removes the element on top of the stack.
void pop() {
que.pop();
}
// Get the top element.
int top() {
return que.front();
}
// Return whether the stack is empty.
bool empty() {
return que.empty();
}
};
Implement Stack using Queues leetcode的更多相关文章
- Implement Stack using Queues ——LeetCode
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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) -- ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
随机推荐
- HDU-2077-汉诺塔IV
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2077 这题就我个人认为比较难,做了很久,递推关系式一直找不出正确的来,找很多递推关系,提交了很多次,全 ...
- cgLib生成动态代理
package com.stono.cglib; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer; import ...
- JSP EL表达式使用
JSP EL表达式使用: Servlet: package com.stono.servlet; import java.io.IOException; import java.util.HashMa ...
- svn git协同管理
项目开发过程中总有一些奇奇怪怪的需求出现. 我们的项目管理是使用SVN的,用SVN是历史原因,无法整个项目向Git切换.由于我需要管理SVN,做一些代码合并工作.每次合并都会遇到SVN代码需要对比查看 ...
- 用虚拟 router 连通 subnet - 每天5分钟玩转 OpenStack(141)
Neutron Routing 服务提供跨 subnet 互联互通的能力.例如前面我们搭建了实验环境: cirros-vm1 172.16.100.3 vlan100 cirr ...
- ACM 刷题小技巧【转】
转载自URl-team ACM做题过程中的一些小技巧. 1.一般用C语言节约空间,要用C++库函数或STL时才用C++; cout.cin和printf.scanf最好不要混用. 大数据输入输出时最好 ...
- 如何用JS/HTML将时间戳转换为“xx天前”的形式【附源码,转
如果我们有一份过去时间戳,如何使用JS/HTML将时间戳转换为"xx天前"的形式呢,以下是完整代码 <!DOCTYPE html> <html> <h ...
- 使用EF扩展EntityFramework.BulkInsert实现批量插入
EntityFramework 最被人诟病的地方就是它的性能,处理大量数据时的效率.此种条件下,通常会转回使用 ADO.NET 来完成任务.而EntityFramework.BulkInsert则是利 ...
- 2017<java技术>预备作业计科冀浩然
1.阅读邹欣老师的博客,谈谈你期望的师生关系是什么样的? 我期望的师生关系是相互融洽的,老师能够在上课的时候尽量多的教我们专业知识,可以尽量多和我们进行互动,课下能和我们如同朋友一般就可以了. 2.你 ...
- openstack安装文档
#########################################openstack m版本部署安装################################## 控制节点.网络 ...