当前位置: 首页 > 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);}
}
http://www.xdnf.cn/news/319195.html

相关文章:

  • 如何高效实现「LeetCode25. K 个一组翻转链表」?Java 详细解决方案
  • SENSE2020BSI sCMOS科学级相机主要参数及应用场景
  • Azure OpenAI 聊天功能全解析:Java 开发者指南
  • 本地部署 MySQL + Qwen3-1.5B + Flask + Dify 工作流
  • 滑动窗口——长度最小子数组
  • var、let、const的区别
  • 高并发内存池(一):项目简介+定长内存池的实现
  • ACE-Step - 20秒生成4分钟完整歌曲,音乐界的Stable Diffusion,支持50系显卡 本地一键整合包下载
  • MySQL 8.0 OCP(1Z0-908)英文题库(1-10)
  • PyTorch常用命令(可快速上手PyTorch的核心功能,涵盖从数据预处理到模型训练的全流程)
  • 【RabbitMQ可靠性原理】
  • 亚远景-ASPICE vs ISO 21434:汽车软件开发标准的深度对比
  • YOLOv8的Python基础--函数篇2
  • WordPress:Locoy.php火车头采集
  • 【HTTP】《HTTP 全原理解析:从请求到响应的奇妙之旅》
  • 【MongoDB篇】MongoDB的副本集操作!
  • 数据清洗-电商双11美妆数据分析(二)
  • 5G赋能农业物联网:智能化种植的新纪元
  • JavaWeb:MySQL进阶
  • 趣味编程:梦幻万花筒
  • DBa作业
  • MCP认证全解析:从零到微软认证专家
  • (eNSP)策略路由实验配置
  • Selenium Web自动化测试学习笔记(二)--八大元素定位
  • 详细剖析传输层协议(TCP和UDP)
  • Baumer工业相机堡盟工业相机如何通过BGAPI SDK在Linux系统下设置多个USB相机(C++)
  • 3、食品包装控制系统 - /自动化与控制组件/food-packaging-control
  • 如何在Ubuntu上安装NVIDIA显卡驱动?
  • leetcode 141. Linked List Cycle
  • AtCoder Regular Contest 197 Div2 A,B题解