题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
package main // 采用双栈法记录到当前元素为栈顶的时候,最小的元素是哪个 var stack []int = []int{} var minStack []int = []int{} //记录1中每个元素为栈顶的时候最小值分别是 func Push(node int) { // write code here stack = append(stack, node) if len(minStack) == 0 || minStack[len(minStack)-1] > node { minStack = append(minStack, node) } else { minStack = append(minStack, minStack[len(minStack)-1]) } } func Pop() { // write code here stack = stack[:len(stack)-1] minStack = minStack[:len(minStack)-1] } func Top() int { return stack[len(stack)-1] } func Min() int { // write code here return minStack[len(minStack)-1] }