剑指 offer#09. Queue with two stacks

Topic link

Title description:

Implement a queue with two stacks. The following statement queue, implement its two functions appendTailand deleteHead, respectively, to complete the insertion and deletion integer integer in the tail of the queue in the head of the queue function. (If there is no element in the queue, the deleteHeadoperation returns -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]

Problem-solving ideas

  First understand the meaning of the string array of the example given by the title: CQueueinitialize, appendTailenqueue, deleteHeaddequeue and return the dequeued element. In other words, the string array is the instruction to record the operation, and the input int array contains the elements that need to be enqueued. Pay attention to the output array here. It does not refer to the status of the elements in the current queue, but only refers to the return of each operation. In fact, there is only deleteHeadreturn.
  The second is to use the dual stack to realize the idea of ​​entering the queue into the queue. To be clear about the characteristics of these two data structures, the stack is first-in-last-out, and the queue is first-in-first-out. Obviously, a single stack can't realize the enqueue and enqueue operation of the queue-so how to use the dual stack? In one example, 1,2,3the elements sequentially into the first stack input, this time inputfrom the top of the stack to the bottom of the stack of elements sequentially 3,2,1. Now I want to achieve the dequeue order 1,2,3, pop the elements of the first stack one by one and push them to the second stack output. At this time output, the elements from the top of the stack to the bottom of 1,2,3the stack are in order. Obviously, the order of popping is at this time 1,2,3, That is, the dual stack is used to realize the queue operation.
  The last is the functional design of the three functions.

  1. The title does not require us to do something during initialization, so the constructor CQueuecan be ignored;
  2. According to the example, we found that entering the team appendTaildoes not require a return value, just entering the team is fine. So one line of stacking code can be solved;
  3. deleteHeadDequeue operation requires a loss analysis, not only requires the element to be dequeued, but also returns the dequeued element, and when the team is empty, it must be returned.-1

    if-else logic

    1. If inputand outputare empty, indicating that the queue is empty at this time, return -1;
    2. Otherwise, no matter inputwhether it is empty or not at this time , as long as it is outputnot empty, it means that outputthere are still unpopulated elements, and the operation is preferred output;
    3. Otherwise, outputempty and inputnot empty, we need inputall the elements pressed into output, and then operate output.

  Now give the c++ and JavaScript version codes~

c++

class CQueue {
    
    
public:
    CQueue() {
    
    

    }
    
    void appendTail(int value) {
    
    
        input.push(value);
    }
    
    int deleteHead() {
    
    
        if(input.empty() && output.empty()) //双栈均为空
            return -1;
        else if(!output.empty()){
    
              //output栈不为空,说明还有未出队的元素
            int result = output.top();
            output.pop();
            return result;
        }
        else{
    
                                 //output栈为空,再执行删除需要input元素出栈入栈
            while(!input.empty()){
    
    
                int value = input.top();
                input.pop();
                output.push(value);
            }

            int result = output.top();
            output.pop();
            return result;
        }
        
    }
private:
    stack<int> input;
    stack<int> output;
};


JavaScript

var CQueue = function() {
    
    
    this.output = [];
    this.input = [];
};

/** 
 * @param {number} value
 * @return {void}
 */
CQueue.prototype.appendTail = function(value) {
    
    
    this.input.push(value);
};

/**
 * @return {number}
 */
CQueue.prototype.deleteHead = function() {
    
    
    const {
    
    input, output} = this;       //es6 解构赋值
    if(!output.length && !input.length){
    
    
        return -1;
    }
    else if(output.length){
    
    
        return output.pop();
    }
    else{
    
    
        while(input.length){
    
    
            output.push(input.pop());
        }
        return output.pop();
    }
};

If there are mistakes or not rigorous, please correct me, thank you very much.
My blog: http://breadhunter.gitee.io

Guess you like

Origin blog.csdn.net/weixin_40807714/article/details/104958644