题解 | #包含min函数的栈#
包含min函数的栈
http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
let stackmin=[]
function push(node)
{
stack1.push(node)
if(stackmin.length==0||stackmin[stackmin.length-1]>=node){
stackmin.push(node)
}
else {
stackmin.push(stackmin[stackmin.length-1])
}
// write code here
}
function pop()
{
stackmin.pop()
return stack1.pop()
// write code here
}
//注意!!此处stackmin.pop()
//若stack中pop的是min,逻辑很直接。若stackpop的不是min,stackmin中重复输入了min
//不影响return min
function top()
{
return stack1[stack1.length-1]
// write code here
}
function min()
{
return stackmin[stackmin.length-1]
// write code here
}
module.exports = {
push : push,
pop : pop,
top : top,
min : min
};