关于go:Leetcode专题数组232用栈实现队列

36次阅读

共计 1139 个字符,预计需要花费 3 分钟才能阅读完成。

leetcode:
https://leetcode.cn/problems/implement-queue-using-stacks/des…
解题思路:

type MyQueue struct {stack []int
    back  []int}

/** Initialize your data structure here. */
func Constructor() MyQueue {
    return MyQueue{stack: make([]int, 0),
        back:  make([]int, 0),
    }
}

/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {for len(this.back) != 0 {val := this.back[len(this.back)-1]
        this.back = this.back[:len(this.back)-1]
        this.stack = append(this.stack, val)
    }
    this.stack = append(this.stack, x)
}

/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {for len(this.stack) != 0 {val := this.stack[len(this.stack)-1]
        this.stack = this.stack[:len(this.stack)-1]
        this.back = append(this.back, val)
    }
    if len(this.back) == 0 {return 0}
    val := this.back[len(this.back)-1]
    this.back = this.back[:len(this.back)-1]
    return val
}

/** Get the front element. */
func (this *MyQueue) Peek() int {for len(this.stack) != 0 {val := this.stack[len(this.stack)-1]
        this.stack = this.stack[:len(this.stack)-1]
        this.back = append(this.back, val)
    }
    if len(this.back) == 0 {return 0}
    val := this.back[len(this.back)-1]
    return val
}

/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {return len(this.stack) == 0 && len(this.back) == 0
}

正文完
 0