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 backpeek/pop from frontsize, 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)

类似上一题的两个栈实现队列,但是有点不同,具体的见下面的注释:

 class Stack {
public:
// Push element x onto stack.
void push(int x) {
q1.push(x);
} // Removes the element on top of the stack.
void pop() {
int tmp = q1.front();//注意这里的实现和双栈实现队列的方式是不相同的
q1.pop();
while(!q1.empty()){
q2.push(tmp);
tmp = q1.front();
q1.pop();
}
swap(q1,q2);
} // Get the top element.
int top() {
int tmp = q1.front();
q1.pop();
while(!q1.empty()){
q2.push(tmp);
tmp = q1.front();
q1.pop();
}
q2.push(tmp);
swap(q1, q2);//用swap即可,无须再向上次那样再倒回去
return tmp;
} // Return whether the stack is empty.
bool empty() {
return q1.empty();
}
private:
queue<int> q1;
queue<int> q2;
};

LeetCode OJ:Implement Stack using Queues(队列实现栈)的更多相关文章

  1. 225 Implement Stack using Queues 队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...

  2. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  3. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  4. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  5. LeetCode 225 Implement Stack using Queues 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  6. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. LeetCode(36)- Implement Stack using Queues

    题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...

  8. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. Leetcode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  10. Java [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

随机推荐

  1. Java命令:Jstack

    jstack是java虚拟机自带的一种堆栈跟踪工具. 功能 jstack用于生成java虚拟机当前时刻的线程快照.线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目 ...

  2. Spring MVC 流程

    1. 检查是否为上传文件. 2. 通过HandlerMapping获取HandlerExecutionChain: DispatcherServlet 中包含:handlerMappings , 遍历 ...

  3. 简单的linux压力测试工具webbench

    wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd we ...

  4. CSS 图像拼合技术

    CSS 图像拼合技术 一.图像拼合 图像拼合就是单个图像的集合. 有许多图像的网页可能需要很长的时间来加载和生成多个服务器的请求. 使用图像拼合会降低服务器的请求数量,并节省带宽. 二.图像拼合 - ...

  5. C#的switch case注意

    参数可以是 switch 语句中的 参数必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型. 所以用string也可以,6666

  6. caffe平台快速搭建:caffe+window7+vs2013

    caffe平台快速搭建:caffe+window7+vs2013 1.caffe-master下载 采用微软提供Windows工具包(caffe-master),下载地址:https://github ...

  7. Spring Boot 中yml配置文件

    步骤一:yml格式 现在大家发现,在springboot里还是要用到配置文件的. 除了使用.properties外,springboot还支持 yml格式. 个人觉得yml格式的可读性和..prope ...

  8. AccessTokens

    https://www.oauth.com/oauth2-servers/access-tokens/ Access tokens are the thing that applications us ...

  9. LA 5135 井下矿工(点—双连通分量模板题)

    https://vjudge.net/problem/UVALive-5135 题意:在一个无向图上选择尽量少的点涂黑,使得任意删除一个点后,每个连通分量至少有一个黑点. 思路: 首先dfs遍历求出割 ...

  10. oracle 顺序号生成函数。仿Sequence

    问题提出自项目中的老代码:一个Bill表,存储所有的表单信息,比如:员工入职单,离职单等等.(别喷,我知道要分多个表.但领导的意愿你是没办法违背的)表单的单据号是以四个字母+年月日+数字顺序号来表示. ...