https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/submissions/
class MinStack {
var main = Array<Int>()
//计入push过的最小值
var sub = Array<Int>()
init() {
}
func push(_ x: Int) {
main.append(x)
if sub.isEmpty || sub.last! >= x{
sub.append(x)
}
}
func pop() {
if main.last! == sub.last! {
sub.removeLast()
}
main.removeLast()
}
func top() -> Int {
return main.last!
}
func min() -> Int {
return sub.last!
}
}
网友评论