代码随想录day9|232用栈实现队列225用队列实现栈|01笔记

78 阅读2分钟
  • 栈与队列理论基础
  • 232用栈实现队列

  • 代码随想录 (programmercarl.com)
  • 逻辑与注意事项

  • 方法是用两个栈来模拟队列,分为in和out,当需要pop时,先从in栈弹出再压入out,最后返回out栈顶。push则只要push到in栈即可。peek操作则可以复用pop,将元素放回即可。判定空则需要判定两个栈。
  • 要注意语言的数据结构和容器的底层实现方法。golang中用切片按照反向顺序遍历就可以实现栈的表达。
  • 解题代码

  •     type MyQueue struct {
            stackIn  []int //输入栈
            stackOut []int //输出栈
        }
        
        func Constructor() MyQueue {
            return MyQueue{
                stackIn:  make([]int, 0),
                stackOut: make([]int, 0),
            }
        }
        
        // 往输入栈做push
        func (this *MyQueue) Push(x int) {
            this.stackIn = append(this.stackIn, x)
        }
        
        // 在输出栈做pop,pop时如果输出栈数据为空,需要将输入栈全部数据导入,如果非空,则可直接使用
        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-- {
                    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 {
            return len(this.stackIn) == 0 && len(this.stackOut) == 0
        }
    
  • 225用队列实现栈

  • 代码随想录 (programmercarl.com)
  • 第一印象

  • 可以使用类似于栈实现队列的方法,创建两个队列,需要pop时,将In队列的元素依次压入Out至最后一个,然后直接弹出即可
  • 解题代码

  •     type MyStack struct {
            queIn []int
            queOut []int
        }
        
        
        func Constructor() MyStack {
            return MyStack{
                queIn: []int{},
                queOut: []int{},
            }
        }
        
        
        func (this *MyStack) Push(x int)  {
            this.queIn = append(this.queIn, x)
        }
        
        
        func (this *MyStack) Pop() int {
            lenIn, lenOut := len(this.queIn), len(this.queOut)
            if lenIn==0 {
                if lenOut == 0 {
                    return -1
                }
                for j:=0;j<lenOut;j++ {
                    this.queIn = append(this.queIn, this.queOut[j])
                }
                lenIn = lenOut
                this.queOut = make([]int, 0)
                lenOut = 0
            }
            for i:=0;i<lenIn-1;i++ {
                this.queOut = append(this.queOut,this.queIn[i])
            }
            this.queIn = this.queIn[lenIn-1:]
            p := this.queIn[0]
            this.queIn = make([]int, 0)
        
            return p
        }
        
        
        func (this *MyStack) Top() int {
            p := this.Pop()
            this.Push(p)
            return p
        }
        
        
        func (this *MyStack) Empty() bool {
            return len(this.queIn) == 0 && len(this.queOut)==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();
         */