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 47 48 49
| type MyStack struct { queue1 *[]int
queue2 *[]int }
func Constructor() MyStack { return MyStack{ queue1: &[]int{}, queue2: &[]int{}, } }
func (this *MyStack) Push(x int) { queue1 := this.queue1 queue2 := this.queue2 *queue2 = append(*queue2, x) if len(*queue1) > 0 { *queue2 = append(*queue2, *queue1...) *queue1 = []int{} } this.queue1, this.queue2 = this.queue2, this.queue1 }
func (this *MyStack) Pop() int { queue1 := this.queue1 ans := (*queue1)[0] (*queue1) = (*queue1)[1:] return ans }
func (this *MyStack) Top() int { queue1 := this.queue1 return (*queue1)[0] }
func (this *MyStack) Empty() bool { queue1 := this.queue1 return len(*queue1) == 0 }
/** * Your MyStack object will be instantiated and called as such: * obj := Constructor(); * obj.Push(x); * param_2 := obj.Pop(); * param_3 := obj.Top(); * param_4 := obj.Empty(); */
|