代码随想录 | Day10 | 2023.08.04

今日题目:

232. 用栈实现队列
225. 用队列实现栈

今日总结

第一天刷栈和队列的题目,题目不难,但是实现的细节很丰富。算是对于之前栈和队列的复习了。栈实现队列不难,队列实现栈需要注意一个队列就能够实现。其中的Move操作需要仔细研究。

232. 用栈实现队列

要点

  1. 两个栈In Out倒来倒去实现先进先出即可
  2. 在从stackIn放元素到stackOut时是倒序放进去的

代码:

type MyQueue struct {
    stackIn  []int //输入栈
    stackOut []int //输出栈
}

func Constructor() MyQueue {
    return MyQueue{
        stackIn:  make([]int, 0),
        stackOut: make([]int, 0),
    }
}

func (this *MyQueue) Push(x int)  {
    this.stackIn = append(this.stackIn, x)
}


func (this *MyQueue) Pop() int {
    inLen, outLen := len(this.stackIn), len(this.stackOut)
    if outLen == 0 {
        if inLen == 0 {
            return -1
        }
        for i := inLen - 1; i >= 0; i-- {   //要反向放入out
            this.stackOut = append(this.stackOut, this.stackIn[i])
        }
        this.stackIn = []int{}
        outLen = len(this.stackOut)
    }
    val := this.stackOut[outLen-1]
    this.stackOut = this.stackOut[:outLen-1]    
    return val
}


func (this *MyQueue) Peek() int {  //只需要访问 不需要弹出
    val := this.Pop()
    if val == -1 {
        return -1
    }
    this.stackOut = append(this.stackOut, val)
    return val
}


func (this *MyQueue) Empty() bool {
    if len(this.stackIn) == 0 && len(this.stackOut) == 0{
        return true
    }
    return false
}


/**
 * 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();
 */

225. 用队列实现栈

要点:

  1. 这种自定义数据结构的题目不要局限于题目给出的方法。
  2. 使用两个队列时,要理解Move操作。实质上就是将queue1的元素反向放入queue2再放回去。
  3. 使用一个队列时,要点就是在弹出时将队列的末尾元素(即栈的top元素)之前的所有元素放置在末尾元素的后方从而让它成为队列的首位,就能进行pop了。
  4. 注意题目要求是使用队列的基本操作,所以不能够直接从队列末尾弹出元素。

代码:两个队列

type MyStack struct {
    //创建两个队列
    queue1 []int  
    queue2 []int //暂存数据
}


func Constructor() MyStack {
    return MyStack{	//初始化
        queue1:make([]int,0),
        queue2:make([]int,0),
    }
}


func (this *MyStack) Push(x int)  {
    this.queue2 = append(this.queue2, x)
    this.Move()
}


func (this *MyStack) Move(){    // 作用是将元素从queue1反向放入queue2从而实现先进后出
    if len(this.queue1) == 0{
        //交换,queue1置为queue2,queue2置为空
        this.queue1,this.queue2 = this.queue2,this.queue1
    }else{
        //queue1元素从头开始一个一个追加到queue2中
            this.queue2 = append(this.queue2,this.queue1[0])  
            this.queue1 = this.queue1[1:]	//去除第一个元素
            this.Move()     //重复
    }
}


func (this *MyStack) Pop() int {
    val := this.queue1[0]
    this.queue1 = this.queue1[1:]	//去除第一个元素
    return val
}


func (this *MyStack) Top() int {
    return this.queue1[0]
}


func (this *MyStack) Empty() bool {
    return len(this.queue1) == 0
}


/**
 * 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();
 */

代码: 一个队列

type MyStack struct {
    queue  []int
}


func Constructor() MyStack {
    return MyStack{
        queue: make([]int, 0),
    }
}


func (this *MyStack) Push(x int)  {
    this.queue=append(this.queue,x)
}


func (this *MyStack) Pop() int {
    n := len(this.queue)-1 //把最后一个元素之外的所有元素都取出来存放到最后元素的后面
    for n != 0 {
        temp := this.queue[0]
        this.queue = this.queue[1:]
        this.queue =append(this.queue, temp)
        n--
    }
    val := this.queue[0]
    this.queue = this.queue[1:]
    return val
}


func (this *MyStack) Top() int {
    temp := this.Pop()
    this.queue =append(this.queue, temp)
    return temp
}


func (this *MyStack) Empty() bool {
    if len(this.queue) == 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();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值