面试题 03.06. 动物收容所

面试题 03.06. 动物收容所

动物收容所。有家动物收容所只收容狗与猫,且严格遵守“先进先出”的原则。在收养该收容所的动物时,收养人只能收养所有动物中“最老”(由其进入收容所的时间长短而定)的动物,或者可以挑选猫或狗(同时必须收养此类动物中“最老”的)。换言之,收养人不能自由挑选想收养的对象。请创建适用于这个系统的数据结构,实现各种操作方法,比如enqueue、dequeueAny、dequeueDog和dequeueCat。允许使用Java内置的LinkedList数据结构。

enqueue方法有一个animal参数,animal[0]代表动物编号,animal[1]代表动物种类,其中 0 代表猫,1 代表狗。
dequeue*方法返回一个列表[动物编号, 动物种类],若没有可以收养的动物,则返回[-1,-1]。

示例1:

输入:
[“AnimalShelf”, “enqueue”, “enqueue”, “dequeueCat”, “dequeueDog”, “dequeueAny”]
[[], [[0, 0]], [[1, 0]], [], [], []]
输出:
[null,null,null,[0,0],[-1,-1],[1,0]]

示例2:

输入:
[“AnimalShelf”, “enqueue”, “enqueue”, “enqueue”, “dequeueDog”, “dequeueCat”, “dequeueAny”]
[[], [[0, 0]], [[1, 0]], [[2, 1]], [], [], []]
输出:
[null,null,null,null,[2,1],[0,0],[1,0]]

解题思路

  1. 使用一个链表来完成
class AnimalShelf {
  LinkedList<Animal> animals;

  public AnimalShelf() {
    animals = new LinkedList<>();
  }

  public void enqueue(int[] animal) {
    animals.add(new Animal(animal[0], animal[1]));
  }

  public int[] dequeueAny() {
    if (animals.isEmpty()) {
      return new int[] {-1, -1};
    }
    Animal res = animals.remove(0);
    return new int[] {res._no, res._type};
  }

  public int[] dequeueDog() {
    Animal res = null;
    for (Animal animal : animals) {
      if (animal._type == 1) {
        res = animal;
        break;
      }
    }
    if (res == null) return new int[] {-1, -1};
    animals.remove(res);
    return new int[] {res._no, res._type};
  }

  public int[] dequeueCat() {
    Animal res = null;
    for (Animal animal : animals) {
      if (animal._type == 0) {
        res = animal;
        break;
      }
    }
    if (res == null) return new int[] {-1, -1};
    animals.remove(res);
    return new int[] {res._no, res._type};
  }

  private class Animal {
    int _type;
    int _no;

    Animal(int no, int type) {
      _type = type;
      _no = no;
    }
  }
}
  1. 使用两个链表来完成
class AnimalShelf {
  LinkedList<Integer> cat;
  LinkedList<Integer> dog;

  public AnimalShelf() {
    cat = new LinkedList<>();
    dog = new LinkedList<>();
  }

  public void enqueue(int[] animal) {
    switch (animal[1]) {
      case 0:
        cat.add(animal[0]);
        break;
      case 1:
        dog.add(animal[0]);
        break;
    }
  }

  public int[] dequeueAny() {
    if (!cat.isEmpty() && !dog.isEmpty()) {
      int c = cat.get(0);
      int d = dog.get(0);
      if (c < d) {
        cat.remove(0);
        return new int[] {c, 0};
      } else {
        dog.remove(0);
        return new int[] {d, 1};
      }
    } else if (cat.isEmpty() && !dog.isEmpty()) {
      return dequeueDog();
    } else if (!cat.isEmpty() && dog.isEmpty()) {
      return dequeueCat();
    } else return new int[] {-1, -1};
  }

  public int[] dequeueDog() {
    if (dog.isEmpty()) {
      return new int[] {-1, -1};
    }
    return new int[] {dog.remove(0), 1};
  }

  public int[] dequeueCat() {
    if (cat.isEmpty()) {
      return new int[] {-1, -1};
    }
    return new int[] {cat.remove(0), 0};
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值