Files
LeetCode-Go/leetcode/0232.Implement-Queue-using-Stacks/232. Implement Queue using Stacks.go
2020-08-07 17:06:53 +08:00

52 lines
1.1 KiB
Go

package leetcode
type MyQueue struct {
Stack *[]int
Queue *[]int
}
/** Initialize your data structure here. */
func Constructor232() MyQueue {
tmp1, tmp2 := []int{}, []int{}
return MyQueue{Stack: &tmp1, Queue: &tmp2}
}
/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
*this.Stack = append(*this.Stack, x)
}
/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
if len(*this.Queue) == 0 {
this.fromStackToQueue(this.Stack, this.Queue)
}
popped := (*this.Queue)[len(*this.Queue)-1]
*this.Queue = (*this.Queue)[:len(*this.Queue)-1]
return popped
}
/** Get the front element. */
func (this *MyQueue) Peek() int {
if len(*this.Queue) == 0 {
this.fromStackToQueue(this.Stack, this.Queue)
}
return (*this.Queue)[len(*this.Queue)-1]
}
/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
return len(*this.Stack)+len(*this.Queue) == 0
}
func (this *MyQueue) fromStackToQueue(s, q *[]int) {
for len(*s) > 0 {
popped := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
*q = append(*q, popped)
}
}