简化Peek函数,复用Pop

This commit is contained in:
lingyin
2022-04-06 10:28:57 +08:00
parent c4e11b24ed
commit fa00cd53b5

View File

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