LeetCode 面试题 03.06. 动物收容所

  • 题目
  • 解题
    • 解题一
    • 解题二
    • 解题三

题目


“最老”可以用编号来比较,编号越小,代表越老,题目已经给了动物编号,如果没有需要自己创建编号或者记录时间戳。

"dequeueAny" 是要 dequeue 猫和狗中最老的。

解题

解题一

// javascript
var AnimalShelf = function() {this.queueAnimal = [];
};/** * @param {number[]} animal* @return {void}*/
AnimalShelf.prototype.enqueue = function(animal) {this.queueAnimal.push(animal);
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueAny = function() {let ret = [-1, -1];if (this.queueAnimal.length !== 0) {ret = this.queueAnimal.shift();}return ret;
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueDog = function() {let ret = [-1, -1];for (let index = 0; index < this.queueAnimal.length; index++) {if (this.queueAnimal[index][1] === 1) {ret = this.queueAnimal.splice(index, 1)[0];break;}}return ret;
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueCat = function() {let ret = [-1, -1];for (let index = 0; index < this.queueAnimal.length; index++) {if (this.queueAnimal[index][1] === 0) {ret = this.queueAnimal.splice(index, 1)[0];break;}}return ret;
};

需要注意的是,JS 中 Array 的 splice 函数返回的是一个包含所有被删除元素的数组,本身这里存储的每个元素都是 [编号,猫或狗],所以获取被删除元素时要加索引 [0]。

解题二

// javascript
var AnimalShelf = function() {this.cat = [];this.dog = [];
};/** * @param {number[]} animal* @return {void}*/
AnimalShelf.prototype.enqueue = function(animal) {if (animal[1] === 0) {this.cat.push(animal);}else {this.dog.push(animal);}
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueAny = function() {if (this.cat.length === 0) return this.dequeueDog();if (this.dog.length === 0) return this.dequeueCat();let catIdx = this.cat[0][0], dogIdx = this.dog[0][0];if (catIdx < dogIdx) {return this.dequeueCat();}else {return this.dequeueDog();}
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueDog = function() {if (this.dog.length === 0) return [-1, -1];return this.dog.shift();
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueCat = function() {if (this.cat.length === 0) return [-1, -1];return this.cat.shift();
};

记录下面的写法纯粹是为了熟悉 array of arrays 的操作:

// javascript
var AnimalShelf = function() {this.queueAnimal = [[], []];
};/** * @param {number[]} animal* @return {void}*/
AnimalShelf.prototype.enqueue = function(animal) {let [order, group] = animal;this.queueAnimal[group].push(order);
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueAny = function() {let [queueCat, queueDog] = this.queueAnimal;if (queueDog.length === 0) return this.dequeueCat(); // 没有待领养的狗if (queueCat.length === 0) return this.dequeueDog(); // 没有待领养的猫const oldestDogIdx = queueDog[0], oldestCatIdx = queueCat[0];// 比较最老的狗和最老的猫谁更老if (oldestDogIdx < oldestCatIdx) {return this.dequeueDog();}else {return this.dequeueCat();}
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueDog = function() {let ret = [-1, -1];let [ , queueDog] = this.queueAnimal;if (queueDog.length !== 0) {ret = [queueDog.shift(), 1];}return ret;
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueCat = function() {let ret = [-1, -1];let [queueCat, ] = this.queueAnimal;if (queueCat.length !== 0) {ret = [queueCat.shift(), 0];}return ret;
};

上述解法有一个要学习的地方是 多赋有意义的变量名,因为 queueAnimal 数组里包含数组,属于 Array of arrays,如果想把编号和类型数组直接存进猫或狗的数组中,会变成 三层数组:queueAnimal 数组由 猫数组 和 狗数组 组成,猫 / 狗数组里的元素也是数组,语义不明显的话写起来容易错,别人读的时候光看见 [0][0][0],也不明白其中含义。

解题三

用数组做 shift 时会把剩余元素都前移一位,效率不高,自己写了一个 LinkedList 的方法。

// javascript
var ListNode = function (val) {this.val = val;this.next = null;
};var AnimalShelf = function() {this.dogHead = this.dogTail = new ListNode(0);this.catHead = this.catTail = new ListNode(0);
};/** * @param {number[]} animal* @return {void}*/
AnimalShelf.prototype.enqueue = function(animal) {let newNode = new ListNode(animal);if (animal[1] === 0) {this.catTail.next = newNode;this.catTail = this.catTail.next;}else {this.dogTail.next = newNode;this.dogTail = this.dogTail.next;}
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueAny = function() {if (this.dogHead.next === null) return this.dequeueCat();if (this.catHead.next === null) return this.dequeueDog();if (this.dogHead.next.val[0] < this.catHead.next.val[0]) {return this.dequeueDog();}else {return this.dequeueCat();}
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueDog = function() {let ret = [-1, -1];if (this.dogHead.next !== null) {ret = this.dogHead.next.val;this.dogHead.next = this.dogHead.next.next;// 如果 队列 空了(只剩下伪头节点),把 tail 指针重新指回 headif (this.dogHead.next === null) {this.dogTail = this.dogHead;}}return ret;
};/*** @return {number[]}*/
AnimalShelf.prototype.dequeueCat = function() {let ret = [-1, -1];if (this.catHead.next !== null) {ret = this.catHead.next.val;this.catHead.next = this.catHead.next.next;if (this.catHead.next === null) {this.catTail = this.catHead;}}return ret;
};

写的时候犯了些错误,一是注意 dequeue 中如果队列空了(只剩下伪头节点),要把 tail 指针重新指回 head,否则 tail 还指着被删除的节点,而 head.next 已经为 null,enqueue 时会往 tail 后面加新节点,head 再也找不到这些节点。

还有大概是脑子糊掉了,竟然写出 this.cat.head 这种鬼 T.T。

LeetCode《程序员面试金典》面试题 03.06. 动物收容所相关推荐

  1. 程序员面试金典 - 面试题 03.06. 动物收容所(队列)

    1. 题目 动物收容所.有家动物收容所只收容狗与猫,且严格遵守"先进先出"的原则. 在收养该收容所的动物时,收养人只能收养所有动物中"最老"(由其进入收容所的时 ...

  2. 程序员面试金典面试题 01.06. 字符串压缩

    前言 本系列文章为<程序员面试金典>刷题笔记. 题目位置:字符串压缩 题集:程序员面试金典 题目 字符串压缩.利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能.比如,字符串a ...

  3. [Leetcode][程序员面试金典][面试题16.11][JAVA][跳水板][数学][动态规划]

    [问题描述][简单] [解答思路] 边界问题 k=0 ,不能产生跳水板,返回空数组 shorter 等于longer,只有一种跳水板,返回longerk 思路 一般情况,k块木板,k种可能 跳水板的长 ...

  4. [Leetcode][程序员面试金典][面试题08.03][JAVA][魔术索引][递归][优化]

    [问题描述][简单] [解答思路] 1. 逐个查找 时间复杂度:O(N) 空间复杂度:O(1) public int findMagicIndex(int[] nums) {for (int i = ...

  5. 程序员面试金典 - 面试题 03.05. 栈排序(两栈)

    1. 题目 栈排序. 编写程序,对栈进行排序使最小元素位于栈顶. 最多只能使用一个其他的临时栈存放数据,但不得将元素复制到别的数据结构(如数组)中. 该栈支持如下操作:push.pop.peek 和 ...

  6. 程序员面试金典 - 面试题 03.01. 三合一(数组栈)

    1. 题目 三合一.描述如何只用一个数组来实现三个栈. 你应该实现push(stackNum, value).pop(stackNum).isEmpty(stackNum).peek(stackNum ...

  7. [Leetcode][程序员面试金典][面试题17.13][JAVA][恢复空格][动态规划][Trie][字符串哈希]

    [问题描述][中等] [解答思路] 1. 动态规划 动态规划流程 第 1 步:设计状态 dp[i] 表示字符串的前 i 个字符的最少未匹配数. 第 2 步:状态转移方程 假设当前我们已经考虑完了前 i ...

  8. 程序员面试金典 - 面试题 03.03. 堆盘子 (vector(stack))

    1. 题目 堆盘子.设想有一堆盘子,堆太高可能会倒下来.因此,在现实生活中,盘子堆到一定高度时,我们就会另外堆一堆盘子. 请实现数据结构SetOfStacks,模拟这种行为.SetOfStacks 应 ...

  9. 程序员面试金典 - 面试题 17.06. 2出现的次数(找递推规律)

    1. 题目 编写一个方法,计算从 0 到 n (含 n) 中数字 2 出现的次数. 示例: 输入: 25 输出: 9 解释: (2, 12, 20, 21, 22, 23, 24, 25)(注意 22 ...

  10. 程序员面试金典 - 面试题 16.06. 最小差(排序+双指针)

    1. 题目 给定两个整数数组a和b,计算具有最小差绝对值的一对数值(每个数组中取一个值),并返回该对数值的差 示例: 输入:{1, 3, 15, 11, 2}, {23, 127, 235, 19, ...

最新文章

  1. 使用.NET自带的类实现DataGrid报表的打印。
  2. Apache Kafka源码分析 – Log Management
  3. 设计模式--Builder
  4. MySQL / 基本架构介绍
  5. jq php异步上传图片,PHP+Ajax实现图片异步上传预览
  6. 动态规划训练11 [String painter HDU - 2476]
  7. python return用法_遗传算法(Python) #4 DEAP框架入门
  8. uuid和python中的uuid.py的使用教程
  9. 写一个带输入输出的存储过程_携程大佬带你写一个可扩展的Spring插件。
  10. error 1044 (42000):access denied for user ''@'localhost' to database 'mysql'
  11. linux mysql 数据库同步
  12. Oracle DB优化-如何看SQL的执行计划+收集表的统计信息
  13. 《JavaScript高效图形编程(修订版)》——导读
  14. oneday2mybatis下载
  15. 电信收费计费系统BI项目
  16. 安卓10源码开发定制(21)GPS定位研究(3)修改GPS定位数据测试gps定位代码
  17. Kotlin 密封类
  18. dux修改index.php,DUX主题修改首页轮播图为通栏模式
  19. Reactjs源码分析
  20. kafka之重新分配分区副本kafka-reassign-partitions命令

热门文章

  1. 不要再说SEO没出路了,SEO的十种赚钱方式让你走上小康
  2. React学习笔记之组价进阶
  3. 机器学习实战——决策树构建过程,信息熵及相关代码
  4. mysql5.018怎样运行_MySQL_asp中使用mysql数据库的注意实现,环境:winxp sp2 ,mysql5.0.18,mysql - phpStudy...
  5. H265码流RTP封装方式详解
  6. postfix+squirrelmail实现邮件服务器
  7. 大厂人脸识别技术100%失灵!道翰天琼认知智能机器人平台API接口大脑为您揭秘。
  8. 五大常用办公软件-office办公软件
  9. Linux高级命令10:远程登录、远程拷贝命令
  10. uniapp支付之App、小程序、H5(微信端)、支付宝、百度支付、头条支付