min_stack

  • 2022-12-14
  • 浏览 (520)

min_stack.go 源码

package main

import (
	"math"
)

//最小栈

type MinStack struct {
	stack []int
	min   []int
}

func Constructor() MinStack {
	return MinStack{[]int{}, []int{math.MaxInt64}}
}

func (this *MinStack) Push(x int) {
	this.stack = append(this.stack, x)
	this.min = append(this.min, min(x, this.min[len(this.min)-1]))
}

func (this *MinStack) Pop() {
	this.stack = this.stack[:len(this.stack)-1]
	this.min = this.min[:len(this.min)-1]
}

func (this *MinStack) Top() int {
	return this.stack[len(this.stack)-1]
}

func (this *MinStack) GetMin() int {
	return this.min[len(this.min)-1]
}

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

你可能感兴趣的文章

largest_rectangle_in_histogram

next_greater_element_i

queue_using_stack

0  赞