码迷,mamicode.com
首页 > 其他好文 > 详细

3.7---猫狗收容所(CC150)

时间:2015-12-21 23:26:24      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

解答的思路:建立一个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;
        
    }
}

 

3.7---猫狗收容所(CC150)

标签:

原文地址:http://www.cnblogs.com/yueyebigdata/p/5065112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!