题解 | #包含min函数的栈#

包含min函数的栈

https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49

package main

var (
	stack    []int
	stackMin []int
)

func Push(node int) {
	// write code here
	stack = append(stack, node)

	if len(stackMin) == 0 {
		stackMin = append(stackMin, node)

		return
	}

	if top := stackMin[len(stackMin)-1]; top > node {
		stackMin = append(stackMin, node)
	} else {
		stackMin = append(stackMin, top)
	}

	return
}
func Pop() {
	// write code here
	stack = stack[:len(stack)-1]
	stackMin = stackMin[:len(stackMin)-1]
}
func Top() int {
	// write code here
	return stack[len(stack)-1]
}
func Min() int {
	// write code here
	return stackMin[len(stackMin)-1]
}

全部评论

相关推荐

09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
秋招的嫡长offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务