当前位置: 首页 > news >正文

面试题 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);}
}

相关文章:

  • Node.js入门指南:开启JavaScript全栈开发之旅
  • C++从入门到实战(十三)C++函数模板与类模板初阶讲解
  • CentOS 7 基础环境安装脚本
  • Milvus 向量数据库详解与实践指南
  • linux(centos)联网情况下部署
  • 【Prometheus】业务指标与基础指标的标签来源差异及设计解析
  • 堆与二叉树——C语言
  • 鸿蒙开发——3.ArkTS声明式开发:构建第一个ArkTS应用
  • Python爬虫(20)Python爬虫数据存储技巧:二进制格式(Pickle/Parquet)性能优化实战
  • 康养休闲旅游住宿服务实训室:构建产教融合新标杆
  • Flowable7.x学习笔记(二十一)查看我的发起
  • 【ArcGIS Pro微课1000例】0068:Pro原来可以制作演示文稿(PPT)
  • 国产Word处理控件Spire.Doc教程:在Java中为Word文本和段落设置边框
  • 使用 Celery + Redis + Eventlet 实现 Python 异步编程(Windows 环境)
  • Python爬虫实战:获取百度学术专题文献数据并分析,为读者课题研究做参考
  • VRM Add-on for Blender 学习笔记
  • 第7章-3 维护索引和表
  • 【Qt】Qt 构建系统详解:qmake 入门到项目实战
  • LVGL-对象 lv_obj_t
  • 基于Qt的app开发第六天
  • 图集︱“中国排面”威武亮相
  • 古埃及展进入百天倒计时,闭幕前168小时不闭馆
  • 经济日报整版聚焦“妈妈岗”:就业路越走越宽,有温度重实效
  • 盖茨:20年内将捐出几乎全部财富,盖茨基金会2045年关闭
  • 大风暴雨致湖南岳阳县6户房屋倒塌、100多户受损
  • 湖南张家界警方公告宣布一名外国人居留许可作废