同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
An animal shelter, which holds only dogs and cats, operates on a strictly"first in, first out" basis. People must adopt either the"oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as enqueue
, dequeueAny
, dequeueDog
, and dequeueCat
. You may use the built-in Linked list data structure.
enqueue
method has a animal
parameter, animal[0]
represents the number of the animal, animal[1]
represents the type of the animal, 0 for cat and 1 for dog.
dequeue*
method returns [animal number, animal type]
, if there's no animal that can be adopted, return [-1, -1]
.
Example1:
Input: ["AnimalShelf", "enqueue", "enqueue", "dequeueCat", "dequeueDog", "dequeueAny"] [[], [[0, 0]], [[1, 0]], [], [], []] Output: [null,null,null,[0,0],[-1,-1],[1,0]]
Example2:
Input: ["AnimalShelf", "enqueue", "enqueue", "enqueue", "dequeueDog", "dequeueCat", "dequeueAny"] [[], [[0, 0]], [[1, 0]], [[2, 1]], [], [], []] Output: [null,null,null,null,[2,1],[0,0],[1,0]]
Note:
class AnimalShelf:
def __init__(self):
self.cats = []
self.dogs = []
def enqueue(self, animal: List[int]) -> None:
if animal[1] == 0:
self.cats.insert(0, animal[0])
else:
self.dogs.insert(0, animal[0])
def dequeueAny(self) -> List[int]:
if len(self.dogs) == 0: return self.dequeueCat()
if len(self.cats) == 0: return self.dequeueDog()
return self.dequeueDog() if self.dogs[-1] < self.cats[-1] else self.dequeueCat()
def dequeueDog(self) -> List[int]:
return [-1, -1] if len(self.dogs) == 0 else [self.dogs.pop(), 1]
def dequeueCat(self) -> List[int]:
return [-1, -1] if len(self.cats) == 0 else [self.cats.pop(), 0]
# Your AnimalShelf object will be instantiated and called as such:
# obj = AnimalShelf()
# obj.enqueue(animal)
# param_2 = obj.dequeueAny()
# param_3 = obj.dequeueDog()
# param_4 = obj.dequeueCat()
class AnimalShelf {
Queue<Integer> cats;
Queue<Integer> dogs;
public AnimalShelf() {
cats = new LinkedList<>();
dogs = new LinkedList<>();
}
public void enqueue(int[] animal) {
if (animal[1] == 0) {
cats.offer(animal[0]);
} else {
dogs.offer(animal[0]);
}
}
public int[] dequeueAny() {
return dogs.isEmpty() ? dequeueCat() : (cats.isEmpty() ? dequeueDog() : (dogs.peek() < cats.peek() ? dequeueDog() : dequeueCat()));
}
public int[] dequeueDog() {
return dogs.isEmpty() ? new int[]{-1, -1} : new int[]{dogs.poll(), 1};
}
public int[] dequeueCat() {
return cats.isEmpty() ? new int[]{-1, -1} : new int[]{cats.poll(), 0};
}
}
/**
* Your AnimalShelf object will be instantiated and called as such:
* AnimalShelf obj = new AnimalShelf();
* obj.enqueue(animal);
* int[] param_2 = obj.dequeueAny();
* int[] param_3 = obj.dequeueDog();
* int[] param_4 = obj.dequeueCat();
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。