mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
简化Peek函数,复用Pop
This commit is contained in:
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user