当前位置: 首页 > news >正文

代码随想录算法训练营第十天 | 232. 用栈实现队列、225. 用队列实现栈、20. 有效的括号、删除字符串中的所有相邻重复项

都很简单不赘述

type MyQueue struct {StackinTop intStackOutTop intStackIn []intStackOut []int
}func Constructor() MyQueue {StackIn := make([]int,0)StackOut := make([]int,0)return MyQueue{StackinTop: 0,StackOutTop:0,StackIn:StackIn,StackOut:StackOut,}
}func (this *MyQueue) Push(x int)  {this.StackIn = append(this.StackIn,x)this.StackinTop++
}func (this *MyQueue) Pop() int {tmp := this.StackIn[0]this.StackIn = this.StackIn[1:]return tmp
}func (this *MyQueue) Peek() int {return this.StackIn[0]
}func (this *MyQueue) Empty() bool {if len(this.StackIn) != 0{return false}else{return true}
}/*** Your MyQueue object will be instantiated and called as such:* obj := Constructor();* obj.Push(x);* param_2 := obj.Pop();* param_3 := obj.Peek();* param_4 := obj.Empty();*/

  

type MyStack struct {Queue1 []intQueue2 []int
}func Constructor() MyStack {queue1:= make([]int,0)queue2:= make([]int,0)return MyStack{Queue1:queue1,Queue2:queue2,}
}func (this *MyStack) Push(x int)  {this.Queue1 = append(this.Queue1,x)
}func (this *MyStack) Pop() int {tmp := this.Queue1[len(this.Queue1) - 1]this.Queue1 = this.Queue1[0:len(this.Queue1) - 1]return tmp
}func (this *MyStack) Top() int {return this.Queue1[len(this.Queue1)-1]
}func (this *MyStack) Empty() bool {if len(this.Queue1) == 0{return true}return false
}/*** Your MyStack object will be instantiated and called as such:* obj := Constructor();* obj.Push(x);* param_2 := obj.Pop();* param_3 := obj.Top();* param_4 := obj.Empty();*/

  

func isValid(s string) bool {stack := make([]rune, 0)runes := []rune(s)for _, i2 := range runes {if i2 == '(' || i2 == '[' || i2 == '{' {stack = append(stack, i2)} else {if  len(stack) == 0|| (i2 == ')' && stack[len(stack)-1] != '(') || (i2 == '}' && stack[len(stack)-1] != '{') || (i2 == ']' && stack[len(stack)-1] != '['){return false}stack = stack[0 : len(stack)-1]}}if len(stack) != 0 {return false}return true
}
func removeDuplicates(s string) string {stack := make([]rune,0)runes := []rune(s)for _,i2 := range runes{if len(stack) != 0 && stack[len(stack) - 1] == i2{stack = stack[0:len(stack) - 1]}else{stack = append(stack,i2)}}return string(stack)
}

  

  

http://www.hskmm.com/?act=detail&tid=18311

相关文章:

  • 2025.9.26总结 - A
  • MySQL性能优化
  • 关于“悬荡悟空”决策机制的简要技术说明
  • 最小二乘问题详解1:线性最小二乘
  • 9月26日
  • 工程监理行业多模态视觉​​​​​​​大模型系统,打造工地行业全场景的监理智能生态
  • 完整教程:【鸿蒙心迹】摸蓝图,打地基
  • 正则表达式
  • LuatOS Air780EPM 实现 HTTP 通信:从原理到代码实践
  • 搜维尔科技:Senseglove Nova 2触觉手套:虚拟训练、VR/AR模拟和研究中的触觉反馈
  • 深入解析:盟接之桥EDI软件:中国制造全球化进程中的连接挑战与路径探索
  • 【STM32H7】基于CubeMX从零开始搭建的HAL库工程模板(包含串口重定向和DSP库)
  • 在Windows架构中安装Miniforge及python环境变量配置
  • 搜维尔科技:Force Dimension Omega力反馈设备遥操作工业机器人
  • 3. Ollama 安装,流式输出,多模态,思考模型 - Rainbow
  • C++程序练习(部分未完全完成)
  • C#性能优化基础:垃圾回收机制
  • 实验报告1
  • 2025.9.26——1蓝
  • 根号
  • 【A】杂题选将
  • 有一个[1,5]的等概率随机函数fx(),在不改变fx()函数的情况下,利用fx()函数做出一个[1,7]的等概率随机函数。
  • WSL2 磁盘清理
  • 质因数分解
  • 关于OneBot的QQ机器人探索2
  • putty
  • 深入解析:PHP 8.0+ 高级特性深度探索:架构设计与性能优化
  • 客服系统源码二次开发
  • 喜讯!狮桥集团成为天津市行政执法监督企业联系点,共筑法治营商新环境!
  • redis实现分布式锁2