博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leet-go]-带min函数的栈
阅读量:2055 次
发布时间:2019-04-28

本文共 1580 字,大约阅读时间需要 5 分钟。

文章目录

定义带min函数的栈数据结构:

  • 实现一个能够得到栈中最小元素的min函数;
  • 调用min、push及pop的时间复杂度为O(1);

题解分析

普通栈的push()和pop()函数的复杂度为O(1),但获取栈最小值min()函数需要遍历整个栈,复杂度为O(N);要将min()函数复杂度降为O(1),需要借助辅助栈:

  • 数据栈All:存储所有元素,实现入栈push()函数、出栈pop()函数、获取栈顶top()函数的正常逻辑;
  • 辅助栈Ordered:存储所有非严格降序元素的子序列(栈All中的最小元素始终对应栈B的栈顶元素。

函数实现:

  • push:把元素压入All中,若元素不大于Ordered.top()中的元素,则也压入Ordered栈中;
  • pop:从All中弹出元素,若Ordered.top()等于弹出元素,则也从Ordered中弹出元素;
  • top:获取All的top元素即可;
  • min:获取Ordered的top元素;

复杂度:

  • 时间:所有操作都为O(1);
  • 空间:最多All与Ordered中包含全部元素,所以为O(N);

代码实现

golang中没有默认的stack库,使用切片模拟(只在尾部插入与删除):

  • 插入时用append;
  • 删除时通过切片操作实现;

栈结构定义:

type MinStack struct {
All [] int Ordered [] int}

在使用栈前,需要先通过Constructor构造出栈:

func Constructor() MinStack {
return MinStack{
All: make([]int, 0), Ordered: make([]int, 0), }}func (this *MinStack) Push(x int) {
this.All = append(this.All, x) if len(this.Ordered)==0 || this.Ordered[len(this.Ordered)-1]>=x{
this.Ordered = append(this.Ordered, x) }}func (this *MinStack) Pop() {
if len(this.All)==0{
return } result := this.All[len(this.All)-1] this.All = this.All[:len(this.All)-1] if result == this.Ordered[len(this.Ordered)-1]{
this.Ordered = this.Ordered[:len(this.Ordered)-1] }}func (this *MinStack) Top() int {
if len(this.All)==0{
return 0 } return this.All[len(this.All)-1]}func (this *MinStack) Min() int {
if len(this.Ordered)==0{
return 0 } return this.Ordered[len(this.Ordered)-1]}func TestStack() {
mStack := Constructor() mStack.Push(-2) mStack.Push(0) mStack.Push(-3) fmt.Println(mStack.Min()) mStack.Pop() fmt.Println(mStack.Top()) fmt.Println(mStack.Min())}

转载地址:http://hwilf.baihongyu.com/

你可能感兴趣的文章
别让自己“墙”了自己
查看>>
让你的网站用上炫酷的中文字体
查看>>
使用 font-spider 对 webfont 网页字体进行压缩
查看>>
云原生服务网格 Istio 1.4 部署指南
查看>>
让 Linux 防火墙新秀 nftables 为你的 VPS 保驾护航
查看>>
Istio 1.4 部署指南
查看>>
贫苦家庭用户的 Envoy xDS 控制平面
查看>>
Kubernetes Pod 网络精髓:pause 容器详解
查看>>
Docker 技术鼻祖 Linux Namespace 入门系列:Namespace API
查看>>
使用 ebpf 深入分析容器网络 dup 包问题
查看>>
Kubelet 中的 “PLEG is not healthy” 到底是个什么鬼?
查看>>
超详细的网络抓包神器 Tcpdump 使用指南
查看>>
从 Kubernetes 资源控制到开放应用模型,控制器的进化之旅
查看>>
从此以后运维与开发过上了没羞没臊的性福生活
查看>>
教你如何优雅地魔改 Grafana 主题,太实用了!
查看>>
让我们来看看回到单体的 Istio 到底该怎么部署
查看>>
超详细的网络抓包神器 tcpdump 使用指南
查看>>
iTerm2 都不会用,还敢自称老司机?(上)
查看>>
两个奇技淫巧,将 Docker 镜像体积减小 99%
查看>>
Istio 1.5 部署指南修正版
查看>>