1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package main
type MyQueue struct { inStack []int outStack []int }
func Constructor() MyQueue { return MyQueue{ inStack: make([]int, 0), outStack: make([]int, 0), } }
func (this *MyQueue) Push(x int) { this.inStack = append(this.inStack, x) }
func (this *MyQueue) Pop() int { if len(this.outStack) == 0 { for len(this.inStack) > 0 { val := this.inStack[len(this.inStack)-1] this.inStack = this.inStack[:len(this.inStack)-1] this.outStack = append(this.outStack, val) } } res := this.outStack[len(this.outStack)-1] this.outStack = this.outStack[:len(this.outStack)-1] return res }
func (this *MyQueue) Peek() int { if len(this.outStack) == 0 { for len(this.inStack) > 0 { val := this.inStack[len(this.inStack)-1] this.inStack = this.inStack[:len(this.inStack)-1] this.outStack = append(this.outStack, val) } } return this.outStack[len(this.outStack)-1] }
func (this *MyQueue) Empty() bool { return len(this.inStack) == 0 && len(this.outStack) == 0 }
|