剑指offer 09. 用两个栈实现队列
作者:互联网
stack1为空且stack2为空则返回-1;stack2为空但stack1不为空时,将stack1出栈加入stack2,然后stack2弹出一个元素;stack1为空但stack2不为空时直接stack2.pop()。
var stack1; var stack2; var CQueue = function() { stack1 = []; stack2 = []; }; /** * @param {number} value * @return {void} */ CQueue.prototype.appendTail = function(value) { stack1.push(value); }; /** * @return {number} */ CQueue.prototype.deleteHead = function() { //若stack1为空 if(stack1.length == 0 && stack2.length == 0) { return -1; }else if(stack2.length == 0) { while(stack1.length != 0){ stack2.push(stack1.pop()); } return stack2.pop(); }else { return stack2.pop(); } }; /** * Your CQueue object will be instantiated and called as such: * var obj = new CQueue() * obj.appendTail(value) * var param_2 = obj.deleteHead() */
标签:return,offer,队列,CQueue,09,pop,var,stack2,stack1 来源: https://www.cnblogs.com/yukinon/p/15208171.html