225 Implement Stack using Queues 队列实现栈
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.
Notes:
You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
详见:https://leetcode.com/problems/implement-stack-using-queues/description/
Java实现:
方法一:两个队列实现,始终保持一个队列为空即可
class MyStack {
private Queue<Integer> queue1;
private Queue<Integer> queue2; /** Initialize your data structure here. */
public MyStack() {
queue1=new LinkedList<Integer>();
queue2=new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
if(queue1.isEmpty()){
queue1.offer(x);
while(!queue2.isEmpty()){
queue1.offer(queue2.poll());
}
}else if(queue2.isEmpty()){
queue2.offer(x);
while(!queue1.isEmpty()){
queue2.offer(queue1.poll());
}
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
if(queue1.isEmpty()){
return queue2.poll();
}else{
return queue1.poll();
}
} /** Get the top element. */
public int top() {
if(queue1.isEmpty()){
return queue2.peek();
}else{
return queue1.peek();
}
} /** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty()&&queue2.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
方法二:一个队列实现栈
class MyStack {
private Queue<Integer> queue; /** Initialize your data structure here. */
public MyStack() {
queue=new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
queue.offer(x);
for(int i=0;i<queue.size()-1;++i){
queue.offer(queue.poll());
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
} /** Get the top element. */
public int top() {
return queue.peek();
} /** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
C++实现:
方法一:两个队列实现,始终保持一个队列为空即可
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
if(que1.empty())
{
que1.push(x);
while(!que2.empty())
{
que1.push(que2.front());
que2.pop();
}
}
else if(que2.empty())
{
que2.push(x);
while(!que1.empty())
{
que2.push(que1.front());
que1.pop();
}
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int val;
if(que1.empty())
{
val=que2.front();
que2.pop();
}
else if(que2.empty())
{
val=que1.front();
que1.pop();
}
return val;
} /** Get the top element. */
int top() {
if(que1.empty())
{
return que2.front();
}
else if(que2.empty())
{
return que1.front();
}
} /** Returns whether the stack is empty. */
bool empty() {
return que1.empty()&&que2.empty();
}
private:
queue<int> que1;
queue<int> que2;
}; /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* bool param_4 = obj.empty();
*/
方法二:一个队列实现栈
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
que.push(x);
for(int i=0;i<que.size()-1;++i)
{
que.push(que.front());
que.pop();
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int val=que.front();
que.pop();
return val;
} /** Get the top element. */
int top() {
return que.front();
} /** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
private:
queue<int> que;
}; /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* bool param_4 = obj.empty();
*/
225 Implement Stack using Queues 队列实现栈的更多相关文章
- 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 ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 225 Implement Stack using Queues(用队列实现栈Medium)
题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- 【LeetCode】225. Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- 【LeetCode】225. Implement Stack using Queues 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- .NET中lock的使用方法及注意事项[转]
标准-->ms官方: http://msdn.microsoft.com/zh-cn/library/c5kehkcz(v=vs.90).aspx A.为什么不要 "lock(this ...
- AIX下RAC搭建 Oracle10G(五)安装oracle、建立监听
AIX下RAC搭建系列 AIX下RAC搭建 Oracle10G(五)安装oracle.建立监听 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 ...
- leveldb学习:DBimpl
leveldb将数据库的有关操作都定义在了DB类,它负责整个系统功能组件的连接和调用.是整个系统的脊柱. level::DB是一个接口类,真正的实如今DBimpl类. 作者在文档impl.html中描 ...
- 对于api安全性的思考
目前的情况下api被很多地方应用,随之而来的是api的安全性问题. 我所认识到的安全性问题有以下几个方面: 1.DDoS(拒绝服务攻击),接口被恶意调用,使真实的用户无法享受到正常畅通的服务. ...
- 嵌入式开发之davinci--- 8148 中dsp在dsp_drv.c中的processdata()加算法出现下边缘条纹问题
(1)问题原因 dsp在alglink_priv.c中做灰度处理发现,下面出现条纹,后面发现是cache 缓存没及时写进内存问题 (2)解决办法 for(frameId=0; frameId<f ...
- cocos2d-x中锚点设置及定位方式
问题 在cocos2d演示样例代码HelloCpp中,为什么要将CCMenu设置位置到CCPointZero,即使CCMenu的锚点是在(0.5, 0.5)? 回答 这是由于CCMenu没有使用锚点进 ...
- redis-3.0.3安装測试
$ tar xzvf redis-3.0.3.tar.gz $ cd redis-3.0.3 $ make //编译 编译完毕进行 $ make test 命令測试 得到例如以下错误信息: c ...
- Arch Linux 下Android 源代码的下载以及编译
之前把公司的开发环境由Ubuntu Kylin 换成了Arch Linux.而Arch 下由于种种问题公司的代码一直编只是去.搞定了之后也一直忘了写下来,希望能给相同在Arch 下做Android 开 ...
- flask的CBV,flash,Flask-Session,及WTForms-MoudelForm
1,CBV: from flask import vews class LoginView(views.MethodView): def get(self): return "雪雪其实也很好 ...
- SpringMVC_架构 --跟海涛学SpringMVC(学习笔记)
重点: 1.工作流程及实现原理 2.配置及使用方法 3.共同函数 前言 1.2.模型: 1.2.1.此处模型使用JavaBean,可能造成JavaBean组件类很庞大,一般现在项目都是采用三层架构,而 ...