包含min函数的栈
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
var stack=[];
var minstack=[];
var minele=null;
function push(node)
{
// write code here
if(minele!==null){
if(node<minele){
minele=node;
}
stack.push(node);
minstack.push(minele);
}else{
minele=node;
stack.push(node);
minstack.push(minele);
}
}
function pop()
{
// write code here
stack.pop();
minstack.pop();
}
function top()
{
// write code here
return stack[stack.length-1];
}
function min()
{
// write code here
return minstack[minstack.length-1];
}