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;
            
        }
    }
  • 相关阅读:
    java返回json数据日期为一串数字字符串 js 转义
    ==和equals以及hashcode
    【线程分析】
    【dubbo&zookeeper】
    线程安全实现方案
    IOC原理
    java锁
    java特殊运算符
    HashMap原理和TreeMap原理
    volatile与synchronized
  • 原文地址:https://www.cnblogs.com/yueyebigdata/p/5065112.html
Copyright © 2011-2022 走看看