福州公司网站开发方案,微商的自己做网站叫什么,企业如何做网站外包多少钱,深圳建设网站上市题目来源#xff1a; leetcode题目#xff0c;网址#xff1a;面试题 03.06. 动物收容所 - 力扣#xff08;LeetCode#xff09;
解题思路#xff1a; 使用两个队列分别记录猫狗信息。 收容#xff1a;记录该动物是猫还是狗后#xff0c;将猫狗标志修改为收容时间…题目来源 leetcode题目网址面试题 03.06. 动物收容所 - 力扣LeetCode
解题思路 使用两个队列分别记录猫狗信息。 收容记录该动物是猫还是狗后将猫狗标志修改为收容时间然后在根据是猫是狗分别进入不同队列。 收养动物若猫狗队列均为空返回 [-1,-1]若猫队列为空收养狗若狗队列为空收养猫若均不空查看猫队列和狗队列首个元素的收容时间收养较小者。 收养狗若狗队列为空返回 [-1,-1]否则收养队首狗弹出队首元素有将收容时间修改为狗标志后返回。 收养猫若猫队列为空返回 [-1,-1]否则收养队首猫弹出队首元素有将收容时间修改为猫标志后返回。
解题代码
class AnimalShelf {queuevectorint dog;queuevectorint cat;int time0;
public:AnimalShelf() {while(!dog.empty()){dog.pop();}while(!cat.empty()){cat.pop();}int time0;}void enqueue(vectorint animal) {if(animal[1]0){ //catanimal[1]time;cat.push(animal);}else{animal[1]time;dog.push(animal);}time;}vectorint dequeueAny() {if(cat.size()0 dog.size()0){vectorint res;res.push_back(-1);res.push_back(-1);return res;}else if(cat.size()0){return dequeueDog();}else if(dog.size()0){return dequeueCat();}vectorint theDogdog.front();vectorint theCatcat.front();if(theCat[1]theDog[1]){return dequeueCat();}else{return dequeueDog();}}vectorint dequeueDog() {if(dog.size()0){vectorint res;res.push_back(-1);res.push_back(-1);return res;}vectorint resdog.front();dog.pop();res[1]1;return res;}vectorint dequeueCat() {if(cat.size()0){vectorint res;res.push_back(-1);res.push_back(-1);return res;}vectorint rescat.front();cat.pop();res[1]0;return res;}
};/*** Your AnimalShelf object will be instantiated and called as such:* AnimalShelf* obj new AnimalShelf();* obj-enqueue(animal);* vectorint param_2 obj-dequeueAny();* vectorint param_3 obj-dequeueDog();* vectorint param_4 obj-dequeueCat();*/ 总结 有点取巧在数据量足够大时收容时间会变为负数从而导致发生错误。 无官方题解。