Implement Queue using Two Stacks

/*
how to use two stacks to implement the queue: offer, poll, peek,size, isEmpty
offer(3) offer(2) poll() offer(1) peek() offer(6) poll() poll()
                    3               2              2       1
in   (3 2)  (1  6)
out  (2)  (3)    6  (1)

stack1(in): is the only stack to store new elements when adding a new element into the queue
stack2(out): is the only stack to pop old element out of the queue.
when stack2 is empty, we move all data from stack1(in) to stack2(out) if any
* */
public class QueueUseStack {
    private Deque<Integer> in ;
    private Deque<Integer> out;
    public QueueUseStack(){
        in = new LinkedList<>();
        out = new LinkedList<>();
    }
    //offer: always goes to the in: o(1)
    public void offer(int val){
        in.push(val);
    }
    /*poll:
    * if the out has items, then pop;
    * else shuffle the in to out, then pop
    * corner cases: make the return type Integer, so the caller could do some checks
    * before pop, if the out still empty, return null
    * */
    public Integer poll(){
        if (out.isEmpty()){
            //shuffle
            shuffle();
        }
        if (out.isEmpty()) return null ;
        return out.pop();
    }
    //the only chance we need to shuffle is when out is empty and the caller is invoking poll or peek
    private void shuffle(){
        while (!in.isEmpty()){
            out.push(in.pop());
        }
    }

    //peek
    public Integer peek(){
        if (out.isEmpty()){
            //shuffle
            shuffle();
        }
        if (out.isEmpty()) return null ;
        return out.peek();
    }

    //size: o(1)
    public int size(){
        return in.size() + out.size();
    }

    //isEmpty: o(1)
    public boolean isEmpty(){
        return in.isEmpty() && out.isEmpty();
    }
}
class TestClass{
    public static void main(String[] args){
        QueueUseStack queueUseStack = new QueueUseStack();
        //offer(3) offer(2) poll() offer(1) peek() offer(6) poll() poll()
        queueUseStack.offer(3);
        queueUseStack.offer(2);
        System.out.println(queueUseStack.poll()); //3
        queueUseStack.offer(1);
        System.out.println(queueUseStack.peek()); //2
        queueUseStack.offer(6);
        System.out.println(queueUseStack.poll()); //2
        System.out.println(queueUseStack.poll());  //1
    }
}

time complexity:

offer(), size(), isEmpty(): o(1)

for poll and peek:

worst case: offer n times, peek/poll only once: o(n)

average case: offer n times, peek/poll n times

2n: offer n times + peek/poll n times

n: shuffle n times: totally there are n items to shuffle

/n: average time complexity for each peek/poll

amortized time complexity: o((2n+n)/n) = o(1)

原文地址:https://www.cnblogs.com/davidnyc/p/8456026.html

时间: 2024-11-05 12:35:00

Implement Queue using Two Stacks的相关文章

[CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Implement Queue using Stacks 用栈来实现队列.

Lintcode40 Implement Queue by Two Stacks solution 题解

[题目描述] As the title described, you should only use two stacks to implement a queue's actions.The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.Both pop and top methods should return t

Implement Queue by Two Stacks

As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. Both pop and top methods should return the va

lintcode 中等题:implement queue by two stacks 用栈实现队列

题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和top方法都应该返回第一个元素的值. 样例 比如push(1), pop(), push(2), push(3), top(), pop(),你应该返回1,2和2 挑战 仅使用两个栈来实现它,不使用任何其他数据结构,push,pop 和 top的复杂度都应该是均摊O(1)的 解题 两个栈stack1

LintCode Implement Queue by Two Stacks

1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值: element = peek 返第一个值: add = offer 加入新值在后面并返回true/false. 做此题时, 第一个stack为基础, 第二个stack为媒介来颠倒顺序, 加时从第一个加, 取时从第二个取. public class Queue { private Stack<In

【LeetCode】232. Implement Queue using Stacks

Implement Queue using Stacks 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 t

leetcode_232——Implement Queue using Stacks(栈与队列)

Implement Queue using Stacks Total Accepted: 5687 Total Submissions: 16629My Submissions Question Solution Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from i

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

232. Implement Queue using Stacks 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 whet

[LeetCode][JavaScript]Implement Queue using Stacks

Implement Queue using Stacks 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 t