猫狗收容所
来源:互联网 发布:客户数据分析的流程 编辑:程序博客网 时间:2024/04/30 02:19
题目描述
有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的。
给定一个操作序列int[][2] ope(C++中为vector<vector<int>>)代表所有事件。若第一个元素为1,则代表有动物进入收容所,第二个元素为动物的编号,正数代表狗,负数代表猫;若第一个元素为2,则代表有人收养动物,第二个元素若为0,则采取第一种收养方式,若为1,则指定收养狗,若为-1则指定收养猫。请按顺序返回收养的序列。若出现不合法的操作,即没有可以符合领养要求的动物,则将这次领养操作忽略。
测试样例:
[[1,1],[1,-1],[2,0],[2,-1]]
返回:[1,-1]-------------------------------------------------------------------------------------
class CatDogAsylum {public: vector<int> asylum(vector<vector<int> > ope) { // write code here vector<int> res;//被收养序列 deque<int> adopt;//收养所的动物dui lie for (decltype(ope.size()) i=0; i<ope.size(); ++i){ if (ope[i][0] == 1){ //有动物进入收养所 adopt.push_back(ope[i][1]); } else if (ope[i][0] == 2){ //有人收养动物 if (ope[i][1] == 0){ if (!adopt.empty()){ //有动物可领养 res.push_back(adopt.front()); adopt.pop_front(); }else continue; }else if (ope[i][1] == 1){ //指定收养狗 if (adopt.empty()) continue; deque<int>::iterator iter; for (iter=adopt.begin(); iter!=adopt.end(); ++iter){ if (*iter > 0){ //正数代表狗 res.push_back(*iter); adopt.erase(iter); break; } } } else if (ope[i][1] == -1){ //指定收养猫 if (adopt.empty()) continue;//不合法操作 deque<int>::iterator iter; for (iter=adopt.begin(); iter!=adopt.end(); ++iter){ if (*iter < 0){ //minus代表狗 res.push_back(*iter); adopt.erase(iter); break; } } } } } return res; }};
0 0
- 猫狗收容所
- 牛客网 | 猫狗收容所
- 猫狗收容所
- 猫狗收容所
- 猫狗收容所
- 猫狗收容所
- 猫狗收容所
- 《程序员面试金典》猫狗收容所
- 《程序员面试金典》--狗猫收容所
- 猫狗收容所之程序员面试经典
- 【程序员面试金典】猫狗收容所
- 以队列的方式实现收容所收养猫和狗
- 程序员面试金典题解 猫狗收容所
- 程序员面试金典——猫狗收容所
- 程序员面试金典:双栈排序、猫狗收容所
- 第六天 用两个栈实现队列+双栈排序+猫狗收容所
- 《Cracking the Coding Interview程序员面试金典》----猫狗收容所
- 犬类收容所人员将7只小狗当狗妈妈面摔死
- make makefile cmake qmake 都是什么,有什么区别?
- ecshop数据库表8(ecs_ad_custom)广告客户表
- LeetCode 132 Palindrome Partitioning II (动态规划)
- 欢迎使用CSDN-markdown编辑器
- Error:Execution failed for task ':im:transformClassesWithDexForDebug'.
- 猫狗收容所
- CodeFoces #377(732A|732B|732C|732D|732E)|贪心
- 关于流的一些看法
- 第五章
- 227. Basic Calculator II
- Linux中设置服务自启动的三种方式
- 关于eclipse无法修改Server Locations选项
- 点击小图显示大图 Alertdialog 全屏都可以
- 利用栈实现计算器,先转换为逆波兰式之后运算