首页 > 其他分享 >2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈的的最大容量 capacity 都相同。实现一个叫「餐盘」的类 DinnerPlates, Di

2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈的的最大容量 capacity 都相同。实现一个叫「餐盘」的类 DinnerPlates, Di

时间:2024-03-09 21:01:01浏览次数:15  
标签:03 int top poppedPos len DinnerPlates go dinnerPlates stack

2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号,

每个栈的的最大容量 capacity 都相同。实现一个叫「餐盘」的类 DinnerPlates,

DinnerPlates(int capacity) - 给出栈的最大容量 capacity,

void push(int val) 将给出的正整数 val 推入 从左往右第一个 没有满的栈,

int pop() 返回 从右往左第一个 非空栈顶部的值,并将其从栈中删除,

如果所有的栈都是空的,请返回 -1。

int popAtStack(int index) - 返回编号 index 的栈顶部的值,并将其从栈中删除,

如果编号 index 的栈是空的,请返回 -1。

输入:["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"][[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]]。

输出:[null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1]。

答案2024-03-09:

来自左程云

灵捷3.5

大体步骤如下:

这个问题需要实现一个类 DinnerPlates,其中含有 ConstructorPushPopPopAtStack 四个方法。这个类可以理解成是具有固定容量的多个栈构成的一种数据结构。根据题目描述和提供的 Go 代码文件,这里来分步骤描述大体过程,然后讨论总的时间复杂度和总的空间复杂度。

1.Constructor:

  • 当创建 DinnerPlates 实例时,通过调用 Constructor 方法初始化一个 DinnerPlates 类型的实例。需要传入一个参数 capacity 表示栈的最大容量。在这个方法中,将 capacity 存储到实例字段中,并初始化 stacktoppoppedPos 三个切片。

2.Push:

  • 当调用 Push 方法时,将给定的整数值 val 推入从左到右第一个没有满的栈。

  • 如果所有栈都已满,应该创建一个新的栈来存储 val

  • 如果有栈未满,则将 val 推入最左侧未满的栈中,并更新 top 数组和 stack 数组。

3.Pop:

  • 当调用 Pop 方法时,应该返回最右侧非空栈顶的值,并将其从栈中删除。如果所有的栈都为空,返回 -1。

  • 如果有非空的栈,应该找到最右侧非空栈并返回它的栈顶的值,然后将其值从栈中删除。

4.PopAtStack:

  • 当调用 PopAtStack 方法时,应该返回指定 index 栈的栈顶的值,并将其从栈中删除。如果指定 index 的栈为空,返回 -1。

  • 需要更新 top 数组和 poppedPos 数组,以确保栈的一致性。

总的时间复杂度:

  • Push 方法的时间复杂度为 O(1)。

  • Pop 方法的时间复杂度为 O(1)。

  • PopAtStack 方法的时间复杂度为 O(log n),其中 n 是被删除的元素的数量。

总的空间复杂度:

  • 需要 O(n) 的空间来存储栈中的所有元素,其中 n 是所有栈的元素数量。

go完整代码如下:

package main

import (
	"fmt"
	"sort"
)

type DinnerPlates struct {
	capacity  int
	stack     []int
	top       []int
	poppedPos []int
}

func Constructor(capacity int) DinnerPlates {
	return DinnerPlates{capacity, []int{}, []int{}, []int{}}
}

func (this *DinnerPlates) Push(val int) {
	if len(this.poppedPos) == 0 {
		pos := len(this.stack)
		this.stack = append(this.stack, val)
		if pos%this.capacity == 0 {
			this.top = append(this.top, 0)
		} else {
			stackPos := len(this.top) - 1
			stackTop := this.top[stackPos]
			this.top[stackPos] = stackTop + 1
		}
	} else {
		pos := this.poppedPos[0]
		this.poppedPos = this.poppedPos[1:]
		this.stack[pos] = val
		index := pos / this.capacity
		stackTop := this.top[index]
		this.top[index] = stackTop + 1
	}
}

func (this *DinnerPlates) Pop() int {
	for len(this.stack) > 0 && len(this.poppedPos) > 0 && this.poppedPos[len(this.poppedPos)-1] == len(this.stack)-1 {
		this.stack = this.stack[:len(this.stack)-1]
		pos := this.poppedPos[len(this.poppedPos)-1]
		this.poppedPos = this.poppedPos[:len(this.poppedPos)-1]
		if pos%this.capacity == 0 {
			this.top = this.top[:len(this.top)-1]
		}
	}
	if len(this.stack) == 0 {
		return -1
	} else {
		pos := len(this.stack) - 1
		val := this.stack[pos]
		this.stack = this.stack[:pos]
		if pos%this.capacity == 0 && len(this.top) > 0 {
			this.top = this.top[:len(this.top)-1]
		} else if len(this.top) > 0 {
			this.top[len(this.top)-1] -= 1
		}
		return val
	}
}

func (this *DinnerPlates) PopAtStack(index int) int {
	if index >= len(this.top) {
		return -1
	}
	stackTop := this.top[index]
	if stackTop < 0 {
		return -1
	}
	this.top[index] = stackTop - 1
	pos := index*this.capacity + stackTop
	i := sort.SearchInts(this.poppedPos, pos)
	this.poppedPos = append(this.poppedPos, 0)
	copy(this.poppedPos[i+1:], this.poppedPos[i:])
	this.poppedPos[i] = pos
	return this.stack[pos]
}

func main() {
	dinnerPlates := Constructor(2)
	dinnerPlates.Push(1)
	dinnerPlates.Push(2)
	dinnerPlates.Push(3)
	dinnerPlates.Push(4)
	dinnerPlates.Push(5)
	fmt.Println(dinnerPlates.PopAtStack(0))
	dinnerPlates.Push(20)
	dinnerPlates.Push(21)
	fmt.Println(dinnerPlates.PopAtStack(0))
	fmt.Println(dinnerPlates.PopAtStack(2))
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
	fmt.Println(dinnerPlates.Pop())
}

在这里插入图片描述

标签:03,int,top,poppedPos,len,DinnerPlates,go,dinnerPlates,stack
From: https://www.cnblogs.com/moonfdd/p/18063285

相关文章

  • P2034 选择数字
    原题链接题解不能连续选k个元素\(\to\)任意每k个元素就有一个不选\(\to\)每k个点就有一个断点\(\to\)每个点都有可能是断点\(\to\)dp求解\(sol.1\)令\(f[i]\)为第i个点为断点且为结尾的最大值则\(f[i]=max(f[j]+sum[j+1,i-1])\)\(sol.2\)至少每隔k个点就取一个......
  • GlasgowSmile-v1
    GlasgowSmile-v1.1一信息收集IP扫描端口扫描80访问目录扫描发现joomla和其目录下有个administrator访问joomla有一个登录框joomla目录下administratorbp爆破密码账密joomlaGotham在administrator   登录后台打开模块编辑index.phpkali看监听......
  • Memberinfo call generic method System.InvalidOperationException: 'Late bound op
    staticvoidMain(string[]args){GenericMethod();LogInfo();}staticvoidGenericMethod(){MethodInfomi=typeof(Program).GetMethod("Echo");Console.WriteLine(mi.IsGenericMethodDefinition);Console.WriteLine(mi.Invoke(......
  • 03_vim编辑器的使用
    vim编辑器的使用1.什么是vim?vim是一个文本编辑器,类似于win上的wps。2.为什么要学习vim?因为几乎每一个发行版都有vim/vi编辑器,嵌入式Linxu上通常也会集成vim。3.vi和vim的关系?vim是vi的加强版。4.怎么打开vi编辑器?直接在控制台输入命令:vifilename如果当前路径没......
  • What is Rust's turbofish
    Haveyoueverheardaboutthe“turbofish”?ItisthatpieceofRustsyntaxthatlookslike ::<SomeType>.InthispostIwilldescribewhatitdoesandhowtouseit.Firstof,ifyouweretowritesomethinglikethis:fnmain(){letnumbers:Ve......
  • Go语言实现设计模式之命令模式
    摘要:命令模式是一种常用的设计模式,它将请求封装成对象,从而使请求的发送者和接收者解耦。本文将详细介绍命令模式的概念和原理,并使用Go语言实现一个示例,以帮助读者更好地理解该设计模式的应用。引言:在软件开发中,命令模式是一种常见的设计模式,它可以帮助我们将请求的发送者和接收......
  • 用lazarus编写的类RichView显示控件初步支持markdown格式的表格,并增加单元格字体颜色
    用lazarus编写的类RichView显示控件初步支持markdown格式的表格,并增加单元格字体颜色等功能,可在信创电脑使用,功能慢慢添加中。github:https://github.com/szlbz/QFComponent其中图像格式支持:bmp,jpg,png等 除以上格式外,还支持单、双分割线等......
  • 【深度解析】'go build'缓存机制:揭秘Windows下缓慢的原因
    引言本文主要围绕gobuild的缓存hash计算与获取缓存文件来编写。  笔者是Windows系统用户,在gobuild或golist-export一些需要编译(但已存在编译缓存)场景下执行的很慢。网上有很多说法大多都是说关闭杀毒软件、关闭磁盘扫描等,并未清楚的描述为什么。  接下来我将围绕g......
  • [STL标准库]240308练习
    标准输入输出#include<iostream>#include<bits/stdc++.h>usingnamespacestd;voidTest1(){ inti;charc;doubled;strings,s1; ios::sync_with_stdio(false);//关闭c语言流的链接 cin.tie(0);//关闭cin和cout的链接 cin>>i>>c>>d>>......
  • 20240308打卡
    第二周第一天第二天第三天第四天第五天第六天第七天所花时间1h5h1h1.5h1h代码量(行)70116628277博客量(篇)11111知识点了解学会详细地全局路由配置有关动态规划算法python基础知识使用json前后端传值存值数据库原理第一章知识整理......