mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
简化Peek函数,复用Pop
This commit is contained in:
@ -275,15 +275,11 @@ func (this *MyQueue) Pop() int {
|
||||
|
||||
/** 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 {
|
||||
val := this.Pop()
|
||||
if val == 0 {
|
||||
return 0
|
||||
}
|
||||
val := this.back[len(this.back)-1]
|
||||
this.back = append(this.back, val)
|
||||
return val
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user