LeetCode series "implement stack with queue"

225. Use queues to implement stacks

In general, there are two methods:

  • 两个Realize with queue
  • 一个Realize with queue

Method 1 : 两个Realize with queue

The best way is to let the two queues q1(in-stack queue) and q2(out-stack queue) act as push-in and out-stack roles respectively, namely:

  • push Add elements toq1
  • popFrom q2the pollelements

q1And q2just a pointer, in pushthe internal time by using a temporary variable to the exchange q1and q2the point, so that q1and q2point to always point to the queue 入栈队列and 出栈队列.

The advantage of this is that you can simplify pop()the top()implementation, as s2is the stack queue, so only needs to be performed separately q2.poll()and q2.peek()can be.

The main reason is push()that the implementation is more complicated. Here are a few pictures to illustrate the process of adding elements:

Insert picture description here

The Java implementation is as follows :

class MyStack {
    
    
  
    private Queue<Integer> q1;
    private Queue<Integer> q2;

    public MyStack() {
    
    
        q1 = new LinkedList<Integer>();
        q2 = new LinkedList<Integer>();
    }
    
  	// 只要是进栈操作就添加到q1,只要是出栈操作就从q2中poll
    public void push(int x) {
    
    
        q1.offer(x);
        while(!q2.isEmpty()){
    
    
            q1.offer(q2.poll());
        }
      	// 通过一个临时的队列来交换q1和q2的指向
        Queue<Integer> temp;   // 这里只是声明一个变量,并没有开辟一个真正的队列空间
        temp = q1;
        q1 = q2;
        q2 = temp;
    }
    
    public int pop() {
    
    
        return q2.poll();
    }
    
    public int top() {
    
    
        return q2.peek();
    }
    
    public boolean empty() {
    
    
        return q1.isEmpty() && q2.isEmpty();
    }
}

Time complexity analysis

  1. push()

Stack operation requires q2the nelement of the team, the team incorporated n+1elements to q1total 2n+1operations, each time a team into the team and the complexity of the operations are O(1), therefore stack operation time complexity O(n).

  • The time complexity of a certain push depends on whether the pop queue is empty. If there is no pop operation (or the number of pops is much less than push), then the complexity of almost every push operation is O(n), because Almost every time q2 is not empty.
  1. The rest areO(1)

Method 2 : 一个Realize with queue

Similarly, when implemented with a queue, the main implementation lies in push()operation.

The specific methods are as follows:

  • Before adding an element each time, write down the number of elements in the current queue asn
  • Then execute to queue.offer(x);add the element toqueue
  • The next more dreamy step is to put queuethe first nelement pollin offerthequeue
  • In this way, the current position at the top of the team must be newly addedx

Here are a few pictures to illustrate the process of adding elements:

The Java implementation is as follows :

class MyStack {
    
    
    Queue<Integer> queue;

    public MyStack() {
    
    
        queue = new LinkedList<Integer>();
    }
    
    public void push(int x) {
    
    
        int n = queue.size();
        queue.offer(x);
        for (int i = 0; i < n; i++) {
    
    
            queue.offer(queue.poll());
        }
    }
    
    public int pop() {
    
    
        return queue.poll();
    }
    
    public int top() {
    
    
        return queue.peek();
    }
   
    public boolean empty() {
    
    
        return queue.isEmpty();
    }
}

Time complexity analysis

  1. push()

Each pushoperation will add n elements to it again queue, so push()the time complexity isO(n)

  1. The rest areO(1)

Guess you like

Origin blog.csdn.net/weixin_44471490/article/details/110648782