读者来到图书馆排队借还书,图书管理员使用两个书车来完成整理借还书的任务。书车中的书从下往上叠加存放,图书管理员每次只能拿取书车顶部的书。排队的读者会有两种操作:
push(bookID)
:把借阅的书籍还到图书馆。
为了保持图书的顺序,图书管理员每次取出供读者借阅的书籍是 最早 归还到图书馆的书籍。你需要返回 每次读者借出书的值 。
如果没有归还的书可以取出,返回 -1
。
type CQueue struct {
outStack []int
inStack []int
}
func Constructor() CQueue {
return CQueue{}
}
func (this *CQueue) AppendTail(value int) {
this.inStack = append(this.inStack, value)
}
func (this *CQueue) DeleteHead() int {
if len(this.outStack) == 0 {
if len(this.inStack) == 0 {
return -1
}
this.InToOut();
}
val := this.outStack[len(this.outStack) - 1]
this.outStack = this.outStack[:len(this.outStack) - 1]
return val
}
func (this *CQueue) InToOut() {
for len(this.inStack) > 0 {
this.outStack = append(this.outStack, this.inStack[len(this.inStack) - 1])
this.inStack = this.inStack[:len(this.inStack) - 1]
}
}