当前位置:网站首页>Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks

Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks

2022-06-25 23:51:00 Program diary

Brush questions these days , Every day I live by reading , Today I have another random question , Queues are implemented with two stacks , It feels good to read the title , But when I look at the input and output samples, I suddenly feel confused , I think this problem may be that the problem looks troublesome, but it's not troublesome to do , Here are the questions :
Topic link : Queues are implemented with two stacks

Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )

Example 1:
Input :
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
Output :[null,null,3,-1]

Example 2:
Input :
[“CQueue”,“deleteHead”,“appendTail”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[],[5],[2],[],[]]
Output :[null,-1,null,null,5,2]

Tips :
1 <= values <= 10000
At most appendTail、deleteHead Conduct 10000 Secondary call

The idea is also simple , Just two stacks ,a The stack is used to store the things in the team , When out of the team , hold a Everything in the stack pops up in turn , Push the b Stack , then b Stack again , Is to go out first , That is to say, FIFO is realized , Then, if you make a count , Join the team again or go to a In the stack , When b After leaving ( It's empty ), And then a Pop up and press in the stack in turn b Stack .
Generally speaking, it is ,a The stack is used to put the value of the queue ,b The stack is used to release the value of the team ,b When the stack is empty a The values in the stack are popped up and pushed in sequence b Just stack .
Here is the code , It is also a question to commemorate the following moments when I have ideas …

import java.util.Stack;

public class CQueue {
    

    Stack<Integer> a;
    Stack<Integer> b;

    public CQueue() {
    
        //  Find two stacks , A stack of data , Another stack makes a queue operation 
        a = new Stack<>();
        b = new Stack<>();
    }

    public void appendTail(int value) {
    
        //  Joining operation 
        a.push(value);
    }

    public int deleteHead() {
    
        if (b.empty()){
    
            int size = a.size();
            // b The stack is empty. , Then put a Put things inside to operate 
            for (int i = 0; i < size; i++){
    
                b.push(a.pop());
            }
        }
        try{
    
            Integer pop = b.pop();
            if (pop != null) return pop;
        }catch (Exception e){
    
            return -1;
        }
        return -1;
    }

}

原网站

版权声明
本文为[Program diary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252101116048.html