JAVA中在某游戏系统有猫狗猪_算法面试题之猫狗队列(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 QueuedogQ;
private QueuecatQ;
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中在某游戏系统有猫狗猪_算法面试题之猫狗队列(java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 江铃福特开启购车补贴,最高优惠 4 万元
- 下一篇: android和windows技术,《技