LeetCode232:Implement Queue using Stacks

原创 2015年07月10日 23:08:22

Implement the following operations of a queue using stacks.

  • push(x) – Push element x to the back of queue.
  • pop() – Removes the element from in front of queue.
  • peek() – Get the front element.
  • empty() – Return whether the queue is empty.

Notes:

  • You must use only standard operations of a stack – which means only
    push to top, peek/pop from top, size, and is empty operations are
    valid.
  • Depending on your language, stack may not be supported natively. You
    may simulate a stack by using a list or deque (double-ended queue),
    as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or
    peek operations will be called on an empty queue).

使用栈来实现一个队列,这个题目之前在《剑指offer》上面见过,没有什么好说的。使用两个栈,一个栈用来保存插入的元素,另外一个栈用来执行pop或top操作,每当执行pop或top操作时检查另外一个栈是否为空,如果为空,将第一栈中的元素全部弹出并插入到第二个栈中,再将第二个栈中的元素弹出即可。需要注意的是这道题的编程时有一个技巧,可以使用peek来实现pop,这样可以减少重复代码。编程时要能扩展思维,如果由于pop函数的声明再前面就陷入用pop来实现peek的功能的话就会感觉无从下手了。

runtime:0ms

class Queue {
public:
    // Push element x to the back of queue.
    void push(int x) {
        pushStack.push(x);
    }

    // Removes the element from in front of queue.
    void pop(void) {
        peek();//这里可以使用peek进行两个栈之间元素的转移从而避免重复代码
        popStack.pop();
    }

    // Get the front element.
    int peek(void) {
        if(popStack.empty())
        {
            while(!pushStack.empty())
            {
                popStack.push(pushStack.top());
                pushStack.pop();
            }
        }
        return popStack.top();
    }

    // Return whether the queue is empty.
    bool empty(void) {
        return pushStack.empty()&&popStack.empty();
    }

    private:
    stack<int> pushStack;//数据被插入到这个栈中
    stack<int> popStack;//数据从这个栈中弹出
};

相关文章推荐

【leetcode】232. Implement Queue using Stacks

一、题目描述 Implement the following operations of a queue using stacks. push(x) -- Push element x...

LeetCode232 Implement Queue using Stacks Java 题解

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

LeetCode232—Implement Queue using Stacks

LeetCode232: Implement Queue using Stacks

LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

翻译用栈来实现队列的下列操作。push(x) —— 将元素x写入到队列的尾部 pop() —— 从队列首部移除元素 peek() —— 返回队列首部元素 empty() —— 返回队列是否为空注意:你...
  • NoMasp
  • NoMasp
  • 2016年01月20日 21:18
  • 2423

[leetcode: Python]232. Implement Queue using Stacks

题目: Implement the following operations of a queue using stacks.push(x) – Push element x to the back...
  • NXHYD
  • NXHYD
  • 2017年05月12日 13:09
  • 142

[LeetCode]232.Implement Queue using Stacks

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

[leetcode] 232. Implement Queue using Stacks

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

leetcode 232. Implement Queue using Stacks

解题思路: 用栈的基本操作实现队列的基本操作 原题目:Implement the following operations of a queue using stacks.push(x) -- P...

Leetcode232. Implement Queue using Stacks

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

leetcode stack 232. Implement Queue using Stacks

法一:使用两个栈模拟队列。class Queue { private: stack q_input; stack q_output; public: // Push eleme...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode232:Implement Queue using Stacks
举报原因:
原因补充:

(最多只允许输入30个字)