面试题 03.06 动物收容所
题目
题解一
使用三个列表,分别保存动物、猫、狗的列表。
package leetcode.editor.cn;import java.util.Iterator;
import java.util.LinkedList;class AnimalShelf {private static final int CATEGORY_CAT = 0;private static final int CATEGORY_DOG = 1;private static final int POS_SEQNO = 0;private static final int POS_CATEGORY = 1;private LinkedList<int[]> animals = new LinkedList<>();private LinkedList<int[]> dogs = new LinkedList<>();private LinkedList<int[]> cats = new LinkedList<>();public AnimalShelf() {}public void enqueue(int[] animal) {if (animal == null || animal.length != 2) {return;}int seqNo = animal[POS_SEQNO];int category = animal[POS_CATEGORY];if (!animals.isEmpty()) {int[] last = animals.getLast();if (seqNo <= last[POS_SEQNO]) {return;}}if (category != CATEGORY_CAT && category != CATEGORY_DOG) {return;}animals.addLast(animal);if (category == CATEGORY_CAT) {cats.addLast(animal);}if (category == CATEGORY_DOG) {dogs.addLast(animal);}}public int[] dequeueAny() {if (animals.isEmpty()) {return new int[] {-1, -1};}int[] animal = animals.getFirst();int category = animal[POS_CATEGORY];if (category == CATEGORY_CAT) {dequeue(animals, cats);}if (category == CATEGORY_DOG) {dequeue(animals, dogs);}return animal;}private int[] dequeue(LinkedList<int[]> first, LinkedList<int[]> last) {if (first.isEmpty()) {return new int [] {-1, -1};}int[] animal = first.pollFirst();int seqNo = animal[POS_SEQNO];Iterator<int[]> iter = last.iterator();while (iter.hasNext()) {int[] animalItem = iter.next();if (animalItem[POS_SEQNO] == seqNo) {iter.remove();break;}}return animal;}public int[] dequeueDog() {if (dogs.isEmpty()) {return new int [] {-1, -1};}return dequeue(dogs, animals);}public int[] dequeueCat() {if (cats.isEmpty()) {return new int [] {-1, -1};}return dequeue(cats, animals);}
}
题解二
使用一个列表,保存动物,使用2个记数器,分别记录猫、狗的数量,避免极端场景下遍历整个列表。
package leetcode.editor.cn;import java.util.Iterator;
import java.util.LinkedList;
class AnimalShelf {private static final int CATEGORY_CAT = 0;private static final int CATEGORY_DOG = 1;private static final int POS_SEQNO = 0;private static final int POS_CATEGORY = 1;private LinkedList<int[]> animals = new LinkedList<>();private int dogCount = 0;private int catCount = 0;public AnimalShelf() {}public void enqueue(int[] animal) {if (animal == null || animal.length != 2) {return;}int seqNo = animal[POS_SEQNO];int category = animal[POS_CATEGORY];if (!animals.isEmpty()) {int[] last = animals.getLast();if (seqNo <= last[POS_SEQNO]) {return;}}if (category != CATEGORY_CAT && category != CATEGORY_DOG) {return;}animals.addLast(animal);if (category == CATEGORY_CAT) {++catCount;}if (category == CATEGORY_DOG) {++dogCount;}}public int[] dequeueAny() {if (animals.isEmpty()) {return new int[]{-1, -1};}int[] animal = animals.pollFirst();int category = animal[POS_CATEGORY];if (category == CATEGORY_CAT) {--catCount;}if (category == CATEGORY_DOG) {--dogCount;}return animal;}public int[] dequeueDog() {if (dogCount <= 0) {return new int[]{-1, -1};}--dogCount;return dequeue(CATEGORY_DOG);}public int[] dequeueCat() {if (catCount <= 0) {return new int[]{-1, -1};}--catCount;return dequeue(CATEGORY_CAT);}private int[] dequeue(int category) {int[] animalItem = new int[]{-1, -1};if (animals.isEmpty()) {return animalItem;}Iterator<int[]> iter = animals.iterator();while (iter.hasNext()) {animalItem = iter.next();if (animalItem[POS_CATEGORY] == category) {iter.remove();break;}}return animalItem;}
}
测试用例
package leetcode.editor.cn;import org.junit.After;
import org.junit.Before;
import org.junit.Test;import static org.junit.Assert.*;public class AnimalShelfTest {private AnimalShelf as = null;@Beforepublic void setUp() throws Exception {as = new AnimalShelf();}@Afterpublic void tearDown() throws Exception {}@Testpublic void test1() {as.enqueue(new int[]{0, 0});as.enqueue(new int[]{1, 0});int[] cat = as.dequeueCat();int[] dog = as.dequeueDog();int[] empty = as.dequeueAny();assertTrue(cat[0] == 0 && cat[1] == 0);assertTrue(dog[0] == -1 && dog[1] == -1);assertTrue(empty[0] == 1 && empty[1] == 0);}@Testpublic void test2() {as.enqueue(new int []{0, 0});as.enqueue(new int []{1, 0});as.enqueue(new int []{2, 1});int[] a1 = as.dequeueDog();int[] a2 = as.dequeueCat();int[] a3 = as.dequeueAny();assertTrue(a1[0] == 2 && a1[1] == 1);assertTrue(a2[0] == 0 && a2[1] == 0);assertTrue(a3[0] == 1 && a3[1] == 0);}@Testpublic void test3() {as.enqueue(new int []{0, 0});as.enqueue(new int []{1, 1});as.enqueue(new int []{2, 0});int[] a1 = as.dequeueAny();int[] a2 = as.dequeueAny();assertTrue(a1[0] == 0 && a1[1] == 0);assertTrue(a2[0] == 1 && a2[1] == 1);}
}