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. Java学习十五

    学习内容: MyBaits 以前从来没有接触过mybatis,通过今天的学习知道这是一个框架,适用于关注SQL优化和需要频繁更新的项目. 今天做一个关于mybatis项目的入门小程序,效果很不理想. ...

  2. 题解【[FJOI2018]所罗门王的宝藏】

    本题解同步于luogu emmm切了近年省选题来写题解啦qwq 该题较其他省选题较水吧(否则我再怎么做的出来 思路是图论做法,做法上楼上大佬已经讲的很清楚了,我来谈谈代码实现上的一些细节 \[\tex ...

  3. 客户主题分析(tableau)—客户分群

    主要分析方面:客户合理分群 客户分群实现:使用聚类构建指标,需理解聚类的分析逻辑,需使用软件:tableau 聚类方法:选择3指标分别为购买总金额,客户购买次数.类平均购买价格(四类的平均购买价格,四 ...

  4. 选择排序&冒泡排序

    //直接选择排序 #include<stdio.h> void SelectionSort(int arr[],int len) { int i,j; int k,min; int tem ...

  5. 微信小程序官方示例 官方weui-wxss下载于安装 详解

    1.小程序示例源码:https://github.com/wechat-miniprogram/miniprogram-demo 2.微信 weui下载地址:https://github.com/we ...

  6. CMS-熊海网站内容管理系统漏洞测试

    开门见山 在虚拟机中搭建网站,本机访问http://192.168.31.68/ 一.SQL注入获取管理员账号密码 1. 点开一篇文章,存在get请求参数 2. 手工注入无果,使用sqlmap,后跟- ...

  7. cppcheck下载及使用

    一.参考文档 1.Ubuntu下安装Cppcheck源码操作步骤 2.cppcheck std.cfg not found error when std.cfg file is available 3 ...

  8. 内存管理-MRC

    MRC内存管理 环境:先关闭arc模式,选中项目->build Settings

  9. Python—插入排序算法

    # 插入排序,时间复杂度O(n²) def insert_sort(arr): """ 插入排序:以朴克牌为例,从小到大排序.摸到的牌current与手里的每张牌进行对比 ...

  10. iterm2 粘贴时有多余字符 0~ 1~

    https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard/issues/31 printf '\e[?2004l'