Programmer interview book, cat and dog shelter, programmer interview book

Source: Internet
Author: User

Programmer interview book, cat and dog shelter, programmer interview book

[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]


Question link: http://www.nowcoder.com/practice/6235a76b1e404f748f7c820583125c50? Rp = 1 & ru =/ta/cracking-the-coding-interview & qru =/ta/cracking-the-coding-interview/question-ranking


Description
There is a home animal shelter that only supports cats and dogs, but there are special adoption rules. There are two adoption methods for the recipient. The first is to directly adopt all the animals to enter the shelter, the second type is the type of the adopted animal (cat or dog), and the earliest adoption of the animal to enter the shelter.
Given an operation sequence int [] [2] ope (vector <int> in C ++) represents all events. If the first element is 1, it indicates that an animal enters the shelter. The second element indicates the number of the animal. An integer indicates the dog. A negative number indicates the cat. If the first element is 2, it indicates that an animal is adopted. If the second element is 0, the first adoption mode is adopted. If it is 1, the dog is adopted. If it is-1, the cat is adopted. Return the adoption sequence in order. If there is an illegal operation, that is, there are no animals that can meet the adoption requirements, the adoption operation will be ignored.
Test example:
[[], [1,-1], [], [2,-1]
Return Value: [1,-1]

Ideas
We use two queues to complete all operations. For the first operation, we only need to return the first element in the queue.
For the second operation, we store all the data that does not meet the requirements into the auxiliary queue, and find the first element that meets the conditions, then delete it, then, all the elements in the auxiliary queue are restored to the queue.


class CatDogAsylum{    public:        vector<int> asylum(vector<vector<int> > ope)        {            // write code here            vector<int> ans;            int len = ope.size();            if(len==0)                return ans;            queue<int> Q1;            queue<int> Q2;            for(int i = 0; i<len; i++)            {                if(ope[i][0]==1)                {                    Q1.push(ope[i][1]);                }                else                {                    if(ope[i][1]==0)                    {                        if(!Q1.empty())                        {                            ans.push_back(Q1.front());                            Q1.pop();                        }                    }                    else                    {                        while(!Q1.empty())                        {                            if(ope[i][1]<0 && Q1.front()<0)                            {                                ans.push_back(Q1.front());                                Q1.pop();                                break;                            }                            else if(ope[i][1]>0 && Q1.front()>0)                            {                                ans.push_back(Q1.front());                                Q1.pop();                                break;                            }                            else                            {                                Q2.push(Q1.front());                                Q1.pop();                            }                        }                        while(!Q1.empty())                        {                            Q2.push(Q1.front());                            Q1.pop();                        }                        while(!Q2.empty())                        {                            Q1.push(Q2.front());                            Q2.pop();                        }                    }                }            }            return ans;        }};


Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.