算法面试题之猫狗队列(java)

【题目】:

已知有宠物:狗、猫如下,实现一种猫狗队列的结构:

/**
 * @ClassName Pet
 * @Description 宠物
 * @Author Huarray
 * @Date 2019/1/10 9:53
 * @Version 1.0
 **/
public class Pet {

    private String type;

    public Pet(String type) {
        this.type = type;
    }

    public String getType() {
        return this.type;
    }

}
/**
 * @ClassName Dog
 * @Description 狗
 * @Author Huarray
 * @Date 2019/1/10 14:48
 * @Version 1.0
 **/
public class Dog extends Pet{

    public Dog(){
        super("Dog");
    }
}
/**
 * @ClassName Cat
 * @Description 猫
 * @Author Huarray
 * @Date 2019/1/10 14:55
 * @Version 1.0
 **/
public class Cat extends Pet {

    public Cat(){
        super("Cat");
    }
}

实现一种猫狗队列结构,要求如下: 
①用户可以调用add方法将cat类或dog类的实例放进队列中; 
②用户可以调用pollAll方法, 将队列中所有的实例按照进队列的先后顺序依次弹出; 
③用户可以调用pollDog方法,将队列中所有的Dog类的实例按照进队列的先后顺序依次弹出; 
④用户可以调用pollCat方法,将队列中所有的Cat类的实例按照进队列的先后顺序依次弹出; 
⑤用户可以调用isEmpty方法,检查队列里,是否还有Dog或Cat类的实例; 
⑥用户可以调用isDogEmpty方法,检查队列里,是否还有Dog类的实例; 
⑦用户可以调用isCatEmpty方法,检查队列里,是否还有Cat类的示例;

针对本题,本文只讲解add和pollAll

思路: 原题中的类是不能进行修改的,所以我们需要设计一个包装类 PetQueue,如下:

这个类有两个特点:1.pet属性,pet属性可以包涵Dog类和Cat类,2.count属性,这个属性是用来记录存入的先后顺序的,方便弹出时做判断

/**
 * @ClassName PetQueue
 * @Description 宠物扩展类
 * @Author Huarray
 * @Date 2019/1/10 15:03
 * @Version 1.0
 **/
public class PetQueue {

    private Pet pet;

    private Long count;

    public PetQueue(Pet pet,Long count){
        this.pet = pet;
        this.count = count;
    }

    public Long getCount(){
        return this.count;
    }

    /**
     * @Author Huarray
     * @Description 获取宠物类型
     * @Date 15:15 2019/1/10
     * @Param []
     * @return java.lang.String
     **/
    public String petQueueType(){
        return this.pet.getType();
    }

    public Pet getPet(){
        return this.pet;
    }
}

这个类便是我们最终要操作的,在add()这个方法中使用switch在性能上比if else 要好,速度更快,这里提一点:当你要判断的类型是字符串类型时,switch处理要迅速,但是如果判断是可变参数时就得用if else了,if else 处理起来更灵活。

import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName DogCatQueue
 * @Description 狗猫集合类
 * @Author Huarray
 * @Date 2019/1/10 14:58
 * @Version 1.0
 **/
public class DogCatQueue {

    private Queue dogQ;

    private Queue catQ;

    private Long count;

    public DogCatQueue(){
        this.dogQ = new LinkedList<>();
        this.catQ = new LinkedList<>();
        this.count = 0L;
    }

    public void add(Pet pet){
        switch (pet.getType()){
            case "Dog":
                this.dogQ.add(new PetQueue(pet,count++));
                break;
            case "Cat":
                this.catQ.add(new PetQueue(pet,count++));
                break;
            default:
                System.out.println("没有该类型的宠物");
                break;
        }
    }

    public PetQueue pollAll(){
        if (!dogQ.isEmpty() || !catQ.isEmpty()){
            if(dogQ.isEmpty()){
                return catQ.poll();
            }
            if (catQ.isEmpty()){
                return dogQ.poll();
            }
            if (dogQ.peek().getCount() < catQ.peek().getCount()){
                return dogQ.poll();
            }else {
                return catQ.poll();
            }
        }
        return null;
    }

}

看测试类:

/**
 * @ClassName Test
 * @Description TODO
 * @Author Huarray
 * @Date 2019/1/10 16:32
 * @Version 1.0
 **/
public class Test {

    public static void main(String[] ages){
        DogCatQueue dc = new DogCatQueue();
        dc.add(new Dog());
        dc.add(new Cat());
        dc.add(new Dog());
        dc.add(new Dog());
        dc.add(new Cat());
        dc.add(new Dog());

        while (true){
            PetQueue petQueue = dc.pollAll();
            if (petQueue != null){
                System.out.println(petQueue.getPet().getType() + "-" + petQueue.getCount());
            }else {
                break;
            }
        }
    }
}

结果:

算法面试题之猫狗队列(java)_第1张图片

如有疑问,欢迎评论,我们一起学习,一起探讨

你可能感兴趣的:(算法)