zoukankan      html  css  js  c++  java
  • 3.7---猫狗收容所(CC150)

    解答的思路:建立一个queue放狗,一个queue放猫。

    如下:

    import java.util.*;
    class Dog{
        int time;
        int value;
        Dog(int a, int b){
            time = a;
            value = b;
        }
    }
    class Cat{
        int time;
        int value;
        Cat(int a, int b){
            time = a;
            value = b;
        }
    }
    
    public class CatDogAsylum {
       
        
        public static ArrayList<Integer> asylum(int[][] ope){
            ArrayList<Integer> res = new ArrayList();
            Queue<Dog> dog = new ArrayDeque();
            Queue<Cat> cat = new ArrayDeque();
            int time = 0;
            for(int i = 0; i < ope.length; i++){
                
                if(ope[i][0] == 1){//push
                    
                    if(ope[i][1] > 0){//dog
                         Dog tmp = new Dog(time++,ope[i][1]);
                        dog.add(tmp);
                        
                    }
                    else if(ope[i][1] < 0){//cat
                         Cat tmp = new Cat(time++,ope[i][1]);
                            cat.add(tmp);
                    }
                }
                
                else if(ope[i][0] == 2){//pop
                    if(ope[i][1] == 0){
                        if(!dog.isEmpty() && ! cat.isEmpty()){
                            if(dog.peek().time < cat.peek().time){
                                res.add(dog.peek().value);
                                dog.poll();
                            }
                            else{
                                res.add(cat.peek().value);
                                cat.poll();
                            }
                        }
                        else if(!dog.isEmpty()){
                            res.add(dog.peek().value);
                            dog.poll();
                        }
                        else {
                            res.add(cat.peek().value);
                            cat.poll();
                        }
                    }
                    else if(ope[i][1] == 1){
                        if(!dog.isEmpty()){
                            res.add(dog.peek().value);
                            dog.poll();
                        }
                    }
                    else if(ope[i][1] == -1){
                        if(!cat.isEmpty()){
                            res.add(cat.peek().value);
                            cat.poll();
                        }
                    }
                    
                }
                
            }
            
            
            return res;
            
        }
    }
  • 相关阅读:
    微软开源全新的文档生成工具DocFX
    .NET平台微服务项目汇集
    谷歌发布的首款基于HTTP/2和protobuf的RPC框架:GRPC
    Microsoft开源跨平台的序列化库——Bond
    Oracle job procedure
    Windows10
    移动端Reactive Native轮播组件
    PHP完整环境搭建
    Webpack 入门
    git 提交
  • 原文地址:https://www.cnblogs.com/yueyebigdata/p/5065112.html
Copyright © 2011-2022 走看看