Design a Phone Directory which supports the following operations:

  1. get: Provide a number which is not assigned to anyone.
  2. check: Check if a number is available or not.
  3. release: Recycle or release a number.

Example:

// Init a phone directory containing a total of 3 numbers: 0, 1, and 2.
PhoneDirectory directory = new PhoneDirectory(3); // It can return any available phone number. Here we assume it returns 0.
directory.get(); // Assume it returns 1.
directory.get(); // The number 2 is available, so return true.
directory.check(2); // It returns 2, the only number that is left.
directory.get(); // The number 2 is no longer available, so return false.
directory.check(2); // Release number 2 back to the pool.
directory.release(2); // Number 2 is available again, return true.
directory.check(2);

又是一道设计题,让我们设计一个电话目录管理系统,可以分配电话号码,查询某一个号码是否已经被使用,释放一个号码。既然要分配号码,肯定需要一个数组 nums 来存所有可以分配的号码,注意要初始化成不同的数字。然后再用一个长度相等的数组 used 来标记某个位置上的号码是否已经被使用过了,用一个变量 idx 表明当前分配到的位置。再 get 函数中,首先判断若 idx 小于0了,说明没有号码可以分配了,返回 -1。否则就取出 nums[idx],并且标记该号码已经使用了,注意 idx 还要自减1,返回之前取出的号码。对于 check 函数,直接在 used 函数中看对应值是否为0。最后实现 release 函数,若该号码没被使用过,直接 return;否则将 idx 自增1,再将该号码赋值给 nums[idx],然后在 used 中标记为0即可,参见代码如下:

解法一:

class PhoneDirectory {
public:
PhoneDirectory(int maxNumbers) {
nums.resize(maxNumbers);
used.resize(maxNumbers);
idx = maxNumbers - ;
iota(nums.begin(), nums.end(), );
}
int get() {
if (idx < ) return -;
int num = nums[idx--];
used[num] = ;
return num;
}
bool check(int number) {
return used[number] == ;
}
void release(int number) {
if (used[number] == ) return;
nums[++idx] = number;
used[number] = ;
} private:
int idx;
vector<int> nums, used;
};

我们也可以使用队列 queue 和 HashSet 来做,整个思想和上面没有啥太大的区别,就是写法上略有不同,参见代码如下:

解法二:

class PhoneDirectory {
public:
PhoneDirectory(int maxNumbers) {
mx = maxNumbers;
for (int i = ; i < maxNumbers; ++i) q.push(i);
}
int get() {
if (q.empty()) return -;
int num = q.front(); q.pop();
used.insert(num);
return num;
}
bool check(int number) {
return !used.count(number);
}
void release(int number) {
if (!used.count(number)) return;
used.erase(number);
q.push(number);
} private:
int mx;
queue<int> q;
unordered_set<int> used;
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/379

参考资料:

https://leetcode.com/problems/design-phone-directory/

https://leetcode.com/problems/design-phone-directory/discuss/85328/Java-AC-solution-using-queue-and-set

https://leetcode.com/problems/design-phone-directory/discuss/122908/Java-O(1)-time-o(n)-space-single-Array-99ms-beats-100

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 379. Design Phone Directory 设计电话目录的更多相关文章

  1. [LeetCode] Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  2. 379. Design Phone Directory

    379. Design Phone Directory Design a Phone Directory which supports the following operations: get: P ...

  3. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  4. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  5. 【LeetCode】379. Design Phone Directory 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组 日期 题目地址:https://leetcode ...

  6. [LeetCode] 362. Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  7. 【LeetCode】Design Linked List(设计链表)

    这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...

  8. LeetCode 707. Design Linked List (设计链表)

    题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...

  9. [LC] 379. Design Phone Directory

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

随机推荐

  1. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  2. java架构之路-(tomcat网络模型)简单聊聊tomcat(二)

    上节课我们说到的Tomcat,并且给予了一般的tomcat配置,和配置的作用,提到了HTTP/1.1 也就是我们的网络通讯模型,那么HTTP/1.1又代表什么呢.我们来简答看一下. tomcat有四种 ...

  3. NaN不等于NaN

    目录 原因 表达式计算 类型转换 总结 不知道这个小知识点用得多不多,曾经在书上看到过,所以有一些印象,前段时间顺手写出类似如下的代码 var result; if (parseInt('abc')= ...

  4. efcore mysql数据库codefirst生成

    添加引用 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Tools Pomelo.EntityFrameworkCore.My ...

  5. Java生鲜电商平台-一次代码重构的实战案例

    Java生鲜电商平台-一次代码重构的实战案例 说明,Java开源生鲜电商平台-一次代码重构的实战案例,根据实际的例子,分析出重构与抽象,使代码更加的健壮与高效. 1.业务说明 系统原先已有登录功能,我 ...

  6. 实战Rest API接口测试

    一.Android App API接口测试 1.如何学好Android App API接口测试 postman可以用来实现API接口自动化测试,但是也有弊端,无法实现接口测试数据的参数化,为了达到接口 ...

  7. 腾讯WeTest亮相—腾讯全球数字生态大会现场

    2019年5月21-23日腾讯全球数字生态大会在云南昆明滇池国际会展中心顺利召开. 此次大会上万人到场参与,大会由主峰会.分论坛.数字生态专题展会以及腾讯数字生态人物颁奖盛典四大板块构成.作为腾讯战略 ...

  8. Laravel向表里插入字段

    执行: php artisan make:migration add_字段_to_表名_table --table=表名 此时在database/migrations文件夹下会生成一个相应文件,更改如 ...

  9. Qt程序app添加图标复制到其它电脑后不显示的解决方法

    原因: 主是要因为Qt显示图标需要依赖一些库来进行转换,而复制到其它电脑后不显示,是没有复制相应的库所致,所以把相应库复制过去就行了. 复制Qt的plugins目录下的imageformats文件到程 ...

  10. 解决Ubuntu在虚拟机窗口不能自适应

    试了很多办法这个好用 相信很多人在装虚拟机的时候,遇到了窗口过小不能自适应的问题.我也是查了好多资料,都说安装Vmware Tools即可解决,还有说修改分辨率也可以.两种方法亲测无效. Vmware ...