class AnimalShelf {
Deque<int[]> any;
Deque<int[]> dog;
Deque<int[]> cat;
public AnimalShelf() {
any = new LinkedList<int[]>();
dog = new LinkedList<int[]>();
cat = new LinkedList<int[]>();
}
public void enqueue(int[] animal) {
if(animal[1] == 0)
cat.addLast(animal);
else
dog.addLast(animal);
any.addLast(animal);
}
public int[] dequeueAny() {
while(true){
if(dog.size() > 0)
if(any.getFirst()[0] == dog.getFirst()[0])
break;
if(cat.size() > 0)
if(any.getFirst()[0] == cat.getFirst()[0])
break;
if(any.size() == 0)
return new int[]{-1,-1};
any.removeFirst();
}
if(dog.size() > 0 && any.getFirst()[0] == dog.getFirst()[0])
dog.removeFirst();
if(cat.size() > 0 && any.getFirst()[0] == cat.getFirst()[0])
cat.removeFirst();
int[] t = any.getFirst();
any.removeFirst();
return t;
}
public int[] dequeueDog() {
if(dog.size() == 0)
return new int[]{-1,-1};
int[] t = dog.getFirst();
dog.removeFirst();
return t;
}
public int[] dequeueCat() {
if(cat.size() == 0)
return new int[]{-1,-1};
int[] t = cat.getFirst();
cat.removeFirst();
return t;
}
}
力扣 面试题 03.06. 动物收容所
最新推荐文章于 2024-08-27 16:11:47 发布