题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param value int整型
* @return 无
*/
const res = []
export function push(value: number) {
// write code here
res.push(value)
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return 无
*/
export function pop() {
// write code here
return res.pop()
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
export function top(): number {
// write code here
return res[res.length - 1]
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
export function min(): number {
// write code here
let minValue = res[0]
for(let i = 1; i < res.length; i++) {
minValue = Math.min(minValue, res[i])
}
return minValue
}
查看23道真题和解析