题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
package main var sta = make([]int, 1) var top1 = 0 var miSt = make([]int, 1) var top2 = 0 func Push(node int) { if len(sta) == top1 { sta = append(sta, node) } else { sta[top1] = node } if top2 == 0 || sta[miSt[top2-1]] > node { if len(miSt) == top2 { miSt = append(miSt, top1) } else { miSt[top2] = top1 } top2++ } top1++ } func Pop() { top1-- if top2 > 0 && miSt[top2-1] == top1 { top2-- } } func Top() int { return sta[top1-1] } func Min() int { return sta[miSt[top2-1]] }