题目
https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
思路
/*** 思路:* 维护两个stack:stack1和stack2* appendTail:直接向stack1中push* deleteHead:* a、如果stack2中有元素,直接pop* b、如果stack2中没有元素,把stack1中的pop出来,再push到stack2中,return stack2.pop()* c、stack2和stack1都是空的直接返回-1*/
解题
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 === 0) {if (this.stack1.length === 0) {return -1}while(this.stack1.length !== 0) {this.stack2.push(this.stack1.pop())}return this.stack2.pop()}return this.stack2.pop()};/*** Your CQueue object will be instantiated and called as such:* var obj = new CQueue()* obj.appendTail(value)* var param_2 = obj.deleteHead()*/
