使用栈来实现队列的如下操作:

push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。
注意:

你只能使用标准的栈操作-- 也就是只有push to top, peek/pop from top, size, 和 is empty 操作是可使用的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque (双端队列)来模拟一个栈,只要你仅使用栈的标准操作就可以。
假设所有操作都是有效的,比如 pop 或者 peek 操作不会作用于一个空队列上。

详见:https://leetcode.com/problems/implement-queue-using-stacks/description/

Java实现:

方法一:

class MyQueue {

    /** Initialize your data structure here. */
private Stack<Integer> stk;
public MyQueue() {
stk=new Stack<Integer>();
} /** Push element x to the back of queue. */
public void push(int x) {
stk.add(0,x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
return stk.pop();
} /** Get the front element. */
public int peek() {
return stk.peek();
} /** Returns whether the queue is empty. */
public boolean empty() {
return stk.isEmpty();
}
} /**
* 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();
* boolean param_4 = obj.empty();
*/

方法二:

class MyQueue {

    /** Initialize your data structure here. */
private Stack<Integer> stkPush;
private Stack<Integer> stkPop;
public MyQueue() {
stkPush=new Stack<Integer>();
stkPop=new Stack<Integer>();
} /** Push element x to the back of queue. */
public void push(int x) {
stkPush.push(x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stkPop.isEmpty()){
while(!stkPush.isEmpty()){
stkPop.push(stkPush.pop());
}
}
return stkPop.pop();
} /** Get the front element. */
public int peek() {
if(stkPop.isEmpty()){
while(!stkPush.isEmpty()){
stkPop.push(stkPush.pop());
}
}
return stkPop.peek();
} /** Returns whether the queue is empty. */
public boolean empty() {
return stkPush.isEmpty()&&stkPop.isEmpty();
}
} /**
* 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();
* boolean param_4 = obj.empty();
*/

C++实现:

class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() { } /** Push element x to the back of queue. */
void push(int x) {
stkPush.push(x);
} /** Removes the element from in front of queue and returns that element. */
int pop() {
if(stkPop.empty())
{
while(!stkPush.empty())
{
stkPop.push(stkPush.top());
stkPush.pop();
}
}
int val=stkPop.top();
stkPop.pop();
return val;
} /** Get the front element. */
int peek() {
if(stkPop.empty())
{
while(!stkPush.empty())
{
stkPop.push(stkPush.top());
stkPush.pop();
}
}
return stkPop.top();
} /** Returns whether the queue is empty. */
bool empty() {
return stkPush.empty()&&stkPop.empty();
}
private:
stack<int> stkPush;
stack<int> stkPop;
}; /**
* 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();
*/

  

232 Implement Queue using Stacks 用栈来实现队列的更多相关文章

  1. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  2. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  4. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  5. 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 ...

  6. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  7. 【LeetCode】232. Implement Queue using Stacks

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

  8. 【一天一道LeetCode】#232. Implement Queue using Stacks

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

  9. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

随机推荐

  1. 网易2018校招 合唱 DP

      时间限制:2秒 空间限制:131072K 小Q和牛博士合唱一首歌曲,这首歌曲由n个音调组成,每个音调由一个正整数表示.对于每个音调要么由小Q演唱要么由牛博士演唱,对于一系列音调演唱的难度等于所有相 ...

  2. IDEA新手下载及和eclipse,myeclipse的区别

    一:下载安装包,我们可以去官网下载.下载地址:http://www.jetbrains.com/idea/download/#secation=windows 二:IDEA的安装: 1.下载安装包后双 ...

  3. Ubuntu 16.04安装RapidSVN

    使用RabbitVCS有一些不完美,比如没有把文件增加到版本库的功能,导致无法提交等问题,现在再次安装RapidSVN来弥补这些缺点. 安装: sudo apt-get install rapidsv ...

  4. u启动为苹果笔记本重装win7系统教程

    准备更换系统的苹果笔记本一台!   上述需要准备的东西均准备好以后我们就开始今天的教程了!!   首先,将已经制作好启动盘的u启动u盘插入到苹果笔记本上的usb插口,然后开机!   由于苹果笔记本电脑 ...

  5. HDOJ 4259 Double Dealing

    找每一位的循环节.求lcm Double Dealing Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. UML——用例图

    用例图是在需求分析阶段开发人员和用户对需求规格达成的某种共识.它描写叙述了待开发系统的功能需求. UML视频使我们对用例图的基本组成元素.属性.粒度等有了理论上的理解,我们还须要自己亲自己主动手画一画 ...

  7. Mariadb 事务

    事务 事务具有ACID特性:原子性(A,atomicity).一致性(C,consistency).隔离性(I,isolation).持久性(D,durabulity). 1.原子性:事务内的所有操作 ...

  8. Netty In Action中文版 - 第四章:Transports(传输)

    本章内容 Transports(传输) NIO(non-blocking IO,New IO), OIO(Old IO,blocking IO), Local(本地), Embedded(嵌入式) U ...

  9. Cocos2d-x3.1下 Android,APK自己主动升级

    项目要做Android的自己主动升级,对于我们之前做iOS的转Cocos开发做Android方面的功能..... 不正确说了.这里记录下我的实现过程. 原文地址:http://blog.csdn.ne ...

  10. easyUI 对话框的关闭事件

    有一个easyUI的dialog: <div id="dlg_Add" class="easyui-dialog" style=" width: ...