Design a Phone Directory which supports the following operations:

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

 public class PhoneDirectory {
Set<Integer> used = new HashSet<>();
Queue<Integer> available = new LinkedList<>();
int max; public PhoneDirectory(int maxNumbers) {
max = maxNumbers;
for (int i = ; i < maxNumbers; i++) {
available.offer(i);
}
} public int get() {
Integer ret = available.poll();
if (ret == null) {
return -;
}
used.add(ret);
return ret;
} public boolean check(int number) {
if (number >= max || number < ) {
return false;
}
return !used.contains(number);
} public void release(int number) {
if (used.remove(number)) {
available.offer(number);
}
}
}

Design Phone Directory的更多相关文章

  1. 379. Design Phone Directory

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

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

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

  3. Leetcode: Design Phone Directory

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

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

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

  5. [LC] 379. Design Phone Directory

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

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

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

  7. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

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

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  9. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

随机推荐

  1. pycharm批量查找替换,正则匹配

    ctrl + r:查找替换 ctrl+f:查找 ctrl+shift+r:全局查找替换 ctrl+alt+f:全局查找 shift+tab将代码左对齐 replace all 完成

  2. 解决xftp远程连接后出现中文乱码

  3. Django基础之JsonResponse对象

    JsonResponse是HttpResponse的子类, 专门用来生成JSON编码的响应. from django.http import JsonResponse response = JsonR ...

  4. DbFunctions 作为 LINQ to Entities 查询的一部分使用时,此方法调用规范 CreateDateTime EDM 函数以创建新的 DateTime 对象。

    DbFunctions.CreateDateTime CreateDateTime(year, month,day,hour,minute,second)

  5. 2 - Rich feature hierarchies for accurate object detection and semantic segmentation(阅读翻译)

    Rich feature hierarchies for accurate object detection and semantic segmentation Ross Girshick Jeff ...

  6. php 设置error_reporting(0)和ini_set('display_errors', 0)之后,还是显示错误

    php 5.4 apache 2.2 关闭错误报告和错误显示 依然会显示错误 按照我的理解,error_reporting(0)之后就应该不会显示错误了,这是怎么回事? 后来我又试着在php.ini者 ...

  7. Java-NIO 之 Selector 与 Pipe

    关于阻塞与非阻塞:https://www.cnblogs.com/jhxxb/p/11272727.html 一.传统的 IO 流都是阻塞式的 当一个线程调用 read() 或 write() 时,该 ...

  8. DataFactory生产手机号码

    表中的数据类型是CHAR()类型的,才会出现,如右图的“Build a composite field”的这个功能: 固定部分设置 剩余变化部分设置 操作成功 数据库查询的结果

  9. nginx详解(代理服务器的解释+nginx 在linux 下的安装+nginx.conf 中的配置解释)

    一.概论 1.什么是代理服务器 代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬 ...

  10. 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变

    带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115   近期有 ...