当前位置: 首页 > 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.rkmt.cn/news/12526.html

相关文章:

  • 关于“悬荡悟空”决策机制的简要技术说明
  • 搜维尔科技:Senseglove Nova 2触觉手套:虚拟训练、VR/AR模拟和研究中的触觉反馈
  • 【STM32H7】基于CubeMX从零开始搭建的HAL库工程模板(包含串口重定向和DSP库)
  • 基于AWS Lambda的机器学习动态定价系统 CI/CD管道部署方案介绍 - 教程
  • 在Windows架构中安装Miniforge及python环境变量配置
  • 3. Ollama 安装,流式输出,多模态,思考模型 - Rainbow
  • 实验报告1
  • 2025.9.26——1蓝
  • 根号
  • 如何在 CentOS 7 上安装 bzip2-libs-1.0.6-13.el7.x86_64.rpm 文件
  • WSL2 磁盘清理
  • 完整教程:1.DHCP服务器
  • 关于OneBot的QQ机器人探索2
  • putty
  • 深入解析:PHP 8.0+ 高级特性深度探索:架构设计与性能优化
  • 完整教程:神经网络torch学习路线规划
  • redis实现分布式锁2
  • 题解:P7334 [JRKSJ R1] 吊打
  • 实用指南:【C++实战㊷】C++ 原型模式实战:从概念到高效应用
  • 警惕新型XCSSET macOS恶意软件变种,专攻Xcode开发者
  • 前端面经-高级开发(华为od) - 实践
  • 垃圾收集器与核心算法详解(上)
  • 【Linux】网络基础 - 实践
  • C# 序列化三种方式
  • VMware+RockyLinux+ikuai+docker+cri-docker+k8s 自用 实践笔记(一) - 详解
  • 记录安装机器/深度学习环境(conda、CUDA、pytorch)时的一些问题
  • 详细介绍:大数据毕业设计选题推荐:基于Hadoop+Spark的全球能源消耗数据分析与可视化系统
  • 深入解析:自动化接口框架搭建分享-pytest
  • 实战需求分析
  • 完整教程:实战:基于 BRPC+Etcd 打造轻量级 RPC 服务——高级特性与生产环境深度实践