1. 程式人生 > >劍指offer(20)包含min函數的棧

劍指offer(20)包含min函數的棧

class write subject 函數 實現 function describe stack title

題目描述:

定義棧的數據結構,請在該類型中實現一個能夠得到棧中所含最小元素的min函數(時間復雜度應為O(1))。

解題代碼:

var stack = [];
function push(node)
{
    // write code here
    stack.push(node);
}
function pop()
{
    // write code here
    return stack.pop();
}
function top()
{
    // write code here
    return stack.length == 0 ? null : stack[0];
}
function min() { // write code here return Math.min.apply(null,stack); }

劍指offer(20)包含min函數的棧