type MyQueue struct {
stackIn []int
stackOut []int
}
func Constructor() MyQueue {
return MyQueue{
stackIn: make([]int, 0),
stackOut: make([]int, 0),
}
}
func (this *MyQueue) Push(x int) {
this.stackIn = append(this.stackIn, x)
}
func (this *MyQueue) Pop() int {
inLen, outLen := len(this.stackIn), len(this.stackOut)
if outLen == 0 {
if inLen == 0 {
return -1
}
for i := inLen - 1; i >= 0; i-- {
this.stackOut = append(this.stackOut, this.stackIn[i])
}
this.stackIn = []int{}
outLen = len(this.stackOut)
}
val := this.stackOut[outLen-1]
this.stackOut = this.stackOut[:outLen-1]
return val
}
func (this *MyQueue) Peek() int {
val := this.Pop()
if val == -1 {
return -1
}
this.stackOut = append(this.stackOut, val)
return val
}
func (this *MyQueue) Empty() bool {
return len(this.stackIn) == 0 && len(this.stackOut) == 0
}
type MyStack struct {
queIn []int
queOut []int
}
func Constructor() MyStack {
return MyStack{
queIn: []int{},
queOut: []int{},
}
}
func (this *MyStack) Push(x int) {
this.queIn = append(this.queIn, x)
}
func (this *MyStack) Pop() int {
lenIn, lenOut := len(this.queIn), len(this.queOut)
if lenIn==0 {
if lenOut == 0 {
return -1
}
for j:=0;j<lenOut;j++ {
this.queIn = append(this.queIn, this.queOut[j])
}
lenIn = lenOut
this.queOut = make([]int, 0)
lenOut = 0
}
for i:=0;i<lenIn-1;i++ {
this.queOut = append(this.queOut,this.queIn[i])
}
this.queIn = this.queIn[lenIn-1:]
p := this.queIn[0]
this.queIn = make([]int, 0)
return p
}
func (this *MyStack) Top() int {
p := this.Pop()
this.Push(p)
return p
}
func (this *MyStack) Empty() bool {
return len(this.queIn) == 0 && len(this.queOut)==0
}