NowCoder

<?php
header("content-type:text/html;charset=utf-8");
/*
 * 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。 P165
 */

$myStack = new SplStack();
$myMin = new SplStack();
function mypush($node)
{
    global $myStack;
    global $myMin;
    $myStack->push($node);
    if($myMin->isEmpty() || $node<=$myMin->top()){
        $myMin->push($node);
    }

}
function mypop()
{
    global $myStack;
    global $myMin;
    if($myStack->top() == $myMin->top()){
        $myMin->pop();
        $myStack->pop();
    }
    else{
        $myStack->pop();
    }
}
function mytop()
{
    global $myStack;
    return $myStack->top();
}
function mymin()
{
    global $myMin;
    return $myMin->top();
}

 

相关文章:

  • 2021-11-09
  • 2021-10-03
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2022-02-03
  • 2021-11-01
  • 2022-01-10
相关资源
相似解决方案