时间复杂度,空间复杂度
package main
type CQueue struct {
inStack, outStack []int
}
func Constructor() CQueue {
return CQueue{}
}
func (this *CQueue) AppendTail(value int) {
this.inStack = append(this.inStack, value)
}
func (this *CQueue) in2out() {
for len(this.inStack) > 0 {
this.outStack = append(this.outStack, this.inStack[len(this.inStack)-1])
this.inStack = this.inStack[:len(this.inStack)-1]
}
}
func (this *CQueue) DeleteHead() int {
if len(this.outStack) == 0 {
if len(this.inStack) == 0 {
return -1
}
this.in2out()
}
value := this.outStack[len(this.outStack)-1]
this.outStack = this.outStack[:len(this.outStack)-1]
return value
}
class CQueue:
def __init__(self):
self.stack = []
self.cache = []
def appendTail(self, value: int) -> None:
self.stack.append(value)
def deleteHead(self) -> int:
if self.cache: return self.cache.pop()
if not self.stack: return -1
while self.stack:
self.cache.append(self.stack.pop())
return self.cache.pop()
public class CQueue
{
private Stack<int> stack;
private Stack<int> cache;
public CQueue()
{
stack = new Stack<int>();
cache = new Stack<int>();
}
public void AppendTail(int value)
{
stack.Push(value);
}
public int DeleteHead()
{
if (cache.Count != 0) {
return cache.Pop();
}
if (stack.Count == 0) {
return -1;
}
while(stack.Count != 0) {
cache.Push(stack.Pop());
}
return cache.Pop();
}
}