232. Implement Queue using Stacks(easy)

Easy

638114FavoriteShare

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.

Example:

MyQueue queue = new MyQueue();

 

queue.push(1);

queue.push(2); 

queue.peek();  // returns 1

queue.pop();   // returns 1

queue.empty(); // returns false

Notes:

  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, 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).

 

双栈法1:

C++:

 

/*
 * @Author: SourDumplings
 * @Date: 2019-08-26 10:51:01
 * @Link: https://github.com/SourDumplings/
 * @Email: [email protected]
 * @Description: https://leetcode.com/problems/implement-queue-using-stacks/
 * 
 * 利用两个栈,每次Push的时候调整,不然peek不好实现,Push O(1)  Pop O(n),利用交换避免复制
 * 调整方法:每次push一个元素前把s1中元素导入到s2中再push元素到s1,再把s2中元素覆盖回s1即可
 */

class MyQueue
{
private:
    stack s1, s2;

public:
    /** Initialize your data structure here. */
    MyQueue()
    {
    }

    /** Push element x to the back of queue. */
    void push(int x)
    {
        while (!s1.empty())
        {
            s2.push(s1.top());
            s1.pop();
        }
        s1.push(x);
        while (!s2.empty())
        {
            s1.push(s2.top());
            s2.pop();
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop()
    {
        int res = s1.top();
        s1.pop();
        return res;
    }

    /** Get the front element. */
    int peek()
    {
        return s1.top();
    }

    /** Returns whether the queue is empty. */
    bool empty()
    {
        return s1.empty();
    }
};

/**
 * 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();
 */

双栈法2:

java:

import java.util.Stack;

/*
 * @Author: SourDumplings
 * @Date: 2019-08-26 11:35:33
 * @Link: https://github.com/SourDumplings/
 * @Email: [email protected]
 * @Description: https://leetcode.com/problems/implement-queue-using-stacks/
 * 
 * s1负责push,s2负责Pop,在连续Pop时做到近似O(1),Push为O(n)
 * peek时所做的调整和pop一样
 */

class MyQueue
{
    private Stack s1 = null;
    private Stack s2 = null;

    /** Initialize your data structure here. */
    public MyQueue()
    {
        s1 = new Stack();
        s2 = new Stack();
    }

    /** Push element x to the back of queue. */
    public void push(int x)
    {
        s1.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop()
    {
        if (s2.isEmpty())
        {
            while (!s1.isEmpty())
            {
                s2.push(s1.pop());
            }
        }

        return s2.pop();
    }

    /** Get the front element. */
    public int peek()
    {
        if (s2.isEmpty())
        {
            while (!s1.isEmpty())
            {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty()
    {
        return s1.isEmpty() && s2.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();
 */

 

你可能感兴趣的:(LeetCode刷题练习)