Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

腾讯&剑指offer09:用两个栈实现队列 #34

Open
sisterAn opened this issue May 7, 2020 · 3 comments
Open

腾讯&剑指offer09:用两个栈实现队列 #34

sisterAn opened this issue May 7, 2020 · 3 comments

Comments

@sisterAn
Copy link
Owner

sisterAn commented May 7, 2020

用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTaildeleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )

示例 1:

输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出:[null,null,3,-1]

示例 2:

输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]

提示:

  • 1 <= values <= 10000
  • 最多会对 appendTaildeleteHead 进行 10000 次调用

leetcode

@shuijingyue
Copy link

var CQueue = function() {
  this.stack1 = []
  this.stack2 = []
};

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

/**
 * @return {number}
 */
CQueue.prototype.deleteHead = function() {
  if (this.stack2.length) {
    return this.stack2.pop();
  } else if (this.stack1.length) {
    while(this.stack1.length) {
      this.stack2.push(this.stack1.pop())
    }
    return this.stack2.pop();
  } else {
    return -1
  }
};

@sisterAn
Copy link
Owner Author

sisterAn commented May 8, 2020

解题思路:

  • 栈后进先出,队列先进先出
  • 双栈可以实现序列倒置:假设有 stack1=[1, 2, 3]stack2=[] ,如果循环出栈 stack1 并将出栈元素进栈 stack2 ,则循环结束后, stack1=[]stack2=[3, 2, 1] ,即通过 stack2 实现了 stack1 中元素的倒置
  • 当需要删除队首元素时,仅仅需要 stack2 出栈即可;当 stack2 为空时,出队就需要将 stack1 元素倒置倒 stack2stack2 再出队即可;如果 stack1 也为空,即队列中没有元素,返回 -1

代码实现:

const CQueue = function() {
    this.stack1 = []
    this.stack2 = []
};
CQueue.prototype.appendTail = function(value) {
    this.stack1.push(value)
};
CQueue.prototype.deleteHead = function() {
    if(this.stack2.length) {
        return this.stack2.pop()
    }
    if(!this.stack1.length) return -1
    while(this.stack1.length) {
        this.stack2.push(this.stack1.pop())
    }
    return this.stack2.pop()
};

复杂度分析:

  • 时间复杂度:appendTail 的时间复杂度为O(1),deleteHead 的时间复杂度为 O(n)
  • 空间复杂度:O(n)

leetcode

@sisterAn sisterAn changed the title 剑指offer09:用两个栈实现队列 腾讯&剑指offer09:用两个栈实现队列 Sep 22, 2020
@dinjufen
Copy link

var CQueue = function() {
    this.s = [];
    this.t = [];
};

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

/**
 * @return {number}
 */
CQueue.prototype.deleteHead = function() {
    if (this.t.length > 0) {
        return this.t.pop();
    } else {
        while (this.s.length > 0) {
            this.t.push(this.s.pop());
        }
        if (this.t.length > 0) {
            return this.t.pop();
        }
    }
    return -1;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants