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);
class PhoneDirectory {

    /** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
private Queue<Integer> queue;
private Set<Integer> used;
public PhoneDirectory(int maxNumbers) {
queue = new LinkedList<>();
used = new HashSet<>();
for (int i = 0; i < maxNumbers; i++) {
queue.offer(i);
}
} /** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
public int get() {
if (queue.isEmpty()) {
return -1;
}
int num = queue.poll();
used.add(num);
return num;
} /** Check if a number is available or not. */
public boolean check(int number) {
return !used.contains(number);
} /** Recycle or release a number. */
public void release(int number) {
if (used.remove(number)) {
queue.offer(number);
}
}
} /**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj.get();
* boolean param_2 = obj.check(number);
* obj.release(number);
*/

[LC] 379. 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] 379. Design Phone Directory 设计电话目录

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

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

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

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

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

  5. Leetcode: Design Phone Directory

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

  6. Design Phone Directory

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

  7. LC 641. Design Circular Deque

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

  8. [LC] 348. Design Tic-Tac-Toe

    Design a Tic-tac-toe game that is played between two players on a nx n grid. You may assume the foll ...

  9. [LC] 362. Design Hit Counter

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

随机推荐

  1. shell的一些一句话东西

    shell的一些一句话东西 2010-09-10 11:22:58|  分类: linux shell |  标签:shell  linux  |举报|字号 订阅     time -p [程序] 可 ...

  2. salt-stack 常用state模块

    /xxx/xxxx/filename: file.managed:                                                       文件管理模块:可以将ma ...

  3. Windows下将Python源代码.py文件封装成exe可执行文件方法

    安装pyinstaller cmd中使用pip安装 pip install pyinstaller 同时会自动安装pywin32(pip真慢50M这里就走20KB),可以进行切换为国内源进行提速. 就 ...

  4. MySQL主主、主从、从从配置文件

    主配置文件: [root@sun01 ~]# more /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql. ...

  5. 自定义的listbox支持拖放

    unit unit2; interface uses Classes, Controls, StdCtrls; type TListBox2 = class(TCustomListBox) prote ...

  6. 实体机安装Ubuntu系统

    今天windows突然蓝屏了,索性安装个 Ubuntu 吧,这次就总结一下实体机安装 Ubuntu 的具体步骤 note: 本人实体机为笔记本 型号为:小米pro U盘为金士顿:8G 安装系统:Ubu ...

  7. Spring注解配置和xml配置优缺点比较

    Spring注解配置和xml配置优缺点比较 编辑 ​ 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...

  8. mysql 不停机 短时间锁表 备份 主备同步 新增备份机器

    刷新数据   [root@localhost ~]# mysql -e 'flush tables with read lock;' 锁表刷新表数据   [root@localhost ~]# mys ...

  9. Dynamics CRM - 通过 C# Plugin 来 abandon Business Process Flow

    需求说明: 当一个 Entity 存在 Business Process Process 时,有时我们需要改变其状态,在之前写的博客有讲了可以通过 JavaScript 来实现,本篇就来讲一下如何通过 ...

  10. \_\_slots\_\_

    __slots__ 一.什么是__slots__ __slots__是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 使用点来访问属性本质就是 ...