输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出: [null,null,3,-1]
输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出: [null,-1,null,null,5,2]
// @algorithm @lc id=100273 lang=javascript
// @title yong-liang-ge-zhan-shi-xian-dui-lie-lcof
var CQueue = function() {
this.s1 = [] // 入队
this.s2 = [] // 出队
};
/**
* @param {number} value
* @return {void}
*/
CQueue.prototype.appendTail = function(value) {
this.s1.push(value)
};
/**
* @return {number}
*/
CQueue.prototype.deleteHead = function() {
if(this.s2.length == 0) {
while(this.s1.length > 0)
this.s2.push(this.s1.pop())
return this.s2.length == 0 ? -1 : this.s2.pop()
} else return this.s2.pop()
};
/**
* Your CQueue object will be instantiated and called as such:
* var obj = new CQueue()
* obj.appendTail(value)
* var param_2 = obj.deleteHead()
*/
// 测试
var obj = new CQueue()
obj.appendTail(3)
obj.appendTail(4)
obj.appendTail(7)
console.log(obj.deleteHead())
console.log(obj.deleteHead())
console.log(obj.deleteHead())
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.min(); --> 返回 -2.
// @algorithm @lc id=100302 lang=javascript
// @title bao-han-minhan-shu-de-zhan-lcof
/**
* initialize your data structure here.
*/
var MinStack = function() {
this.s = []
this.mins = []
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.s.push(x)
if(this.mins.length == 0 || x <= this.mins[this.mins.length - 1]) // push的元素小于当前元素,将其放入mins
this.mins.push(x)
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
let x = this.s.pop()
if(x === this.mins[this.mins.length - 1]) // 如果pop的元素是mins的最后一个元素,则mins也要pop
this.mins.pop()
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.s[this.s.length - 1]
};
/**
* @return {number}
*/
MinStack.prototype.min = function() {
return this.mins[this.mins.length - 1]
};
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.min()
*/
var obj = new MinStack()
obj.push(-2)
obj.push(0)
obj.push(-3)
console.log(obj.min()) // -3
obj.pop()
console.log(obj.top()) // 0
console.log(obj.min()) // -2