猫狗收容所

题目:
有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的。

给定一个操作序列int[][2] ope(C++中为vector<vector>)代表所有事件。若第一个元素为1,则代表有动物进入收容所,第二个元素为动物的编号,正数代表狗,负数代表猫;若第一个元素为2,则代表有人收养动物,第二个元素若为0,则采取第一种收养方式,若为1,则指定收养狗,若为-1则指定收养猫。请按顺序返回收养的序列。若出现不合法的操作,即没有可以符合领养要求的动物,则将这次领养操作忽略。

方法一:list中存储收容所依次收养的猫狗,l中存储收养序列;
将二维数组中的一维数组依次取出,通过数组第一个元素可以判断是收养人收养还是收养所收养,为0则为收容所收养,只需要将第二个元素添加到list中即可;为1则为收养人收养,通过第二个元素判断是那种收养方式,然后通过while循环,找到第一次需要收养的猫或狗,从list中删除;

import java.util.*;

public class CatDogAsylum {
    public ArrayList<Integer> asylum(int[][] ope) {
        ArrayList<Integer> list=new ArrayList<>();
        List<Integer> l=new ArrayList<>();
        for(int[] a:ope){
            if(a[0]==1){
                if(a[1]!=0){
                    l.add(a[1]);
                }
                
            }else{
                if(a[1]==0){
                    list.add(l.remove(0));
                }else if(a[1]==1){
                    int flag=0;
                    int len=l.size();
                    while(flag<len&&l.get(flag)<0){
                        flag++;                                        
                    }
                    if(flag<len){
                        list.add(l.remove(flag));
                    }
                    
                }else{
                    int flag=0;
                    int len=l.size();
                    while(flag<len&&l.get(flag)>0){
                        flag++;
                    }
                    if(flag<len){
                        list.add(l.remove(flag));
                    }
                   
                }
            }
        }
        return list;
    }
}

方法二
animal存储收养序列,cat为收容所依次收养猫的队列,dog为收容所依次收养狗的队列,all是所有动物收养序列;
依次遍历整个二维数组,后面和方法一相似。

import java.util.*;

public class CatDogAsylum {
    public ArrayList<Integer> asylum(int[][] ope) {
        ArrayList<Integer> animal=new ArrayList<>();
        Queue<Integer> cat=new LinkedList<>();
        Queue<Integer> dog=new LinkedList<>();
        Queue<Integer> all=new LinkedList<>();
        for(int i=0;i<ope.length;i++)
        {
            if(ope[i][0]==1)
            {
                all.add(ope[i][1]);
                if(ope[i][1]>0)
                    dog.add(ope[i][1]);
                else if(ope[i][1]<0)
                    cat.add(ope[i][1]);
            }
            else if(ope[i][0]==2)
            {
                if(ope[i][1]==0)
                {
                    if(!all.isEmpty())
                    {
                        int temp=all.poll();
                        animal.add(temp);
                        if(temp>0)
                            dog.poll();
                        else
                            cat.poll();
                    }
                }
                else if(ope[i][1]>0)
                {
                    if(!dog.isEmpty())
                    {
                        int temp=dog.poll();
                        animal.add(temp);
                        all.remove(temp);
                    }
                }
                else
                {
                    if(!cat.isEmpty())
                    {
                        int temp=cat.poll();
                        animal.add(temp);
                        all.remove(temp);
                    }
                }
            }
        }
        return animal;
    }
}
发布了67 篇原创文章 · 获赞 12 · 访问量 1497

猜你喜欢

转载自blog.csdn.net/qq_42174669/article/details/103965524