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. Opencv从文件中播放视频

    1.VideoCapture()括号中写视频文件的名字,在播放每一帧的时候,使用cv2.waitKey()设置适当的持续时间,太低会播放的很快,太高会很慢,通常情况下25毫秒就行了. 2.获取相机/视 ...

  2. 销售de经典语录

    [销售的境界] 1.顾客要的不是便宜,而是感觉上占了便宜: 2.不要与顾客争论价格,要与顾客讨论价值: 3.没有不对的客户,只有不够好的服务: 4.卖什么不重要,重要的是怎么卖: 5.没有最好的产品, ...

  3. 主导SEO成败的关键是细节的布局,细数SEO三大布局思路

    有的人认为SEO操作就类似车间工作,有一个完整的流程,整套流程下来网站就会有一个好的排名.这样是不对的,优化的着重点是要有一个好的思维,技巧和策略,把这些着重点相结合的运用到SEO优化中,很大的机率会 ...

  4. CentOS6.x/6.5/6.4/6.3/6.2/7.x 64位安装php5.2(使用YUM自动安装)

    默认情况下,CentOS6 64 bit 已经早已不支持php5.2.x ,但是某些php程序还需要zend optimizer支持,怎么办呢?目前大部分的yum repos 都已经不支持直接安装ph ...

  5. HTTP Error 500.30 - ANCM In-Process Start Failure错误。.NET Core

    调试.NET Core项目.出现了以下的错误.学网上搞了好久IIS没卵用.然后根据微软的提示,解决了问题. 解决方法: 1. 目标平台换成Any  CPU 2.点击工具-获取工具和功能,把下面这个II ...

  6. Storm 重启排查

    Storm实战常见问题及解决方案 https://www.cnblogs.com/catkins/p/5302634.html Storm 重启排查(续) https://www.iteye.com/ ...

  7. docker---安装docker

    今天开始要初步的学习 docker 了,这也是进入虚拟化方向的一个步骤,学习一个东西首先要从搭建环境开始,哈哈 安装环境及版本: 系统:Ubuntu18.04 STL Docker 版本:19.03. ...

  8. PAT Advanced 1134 Vertex Cover (25) [hash散列]

    题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...

  9. c语言中指针和多维数组的理解

    1.复习指针和数组之间的特殊关系:不带方括号的数组名是一个指针,指向该数组的第一个元素. 2.多维数组: ][];//声明一个二维数组作为举例 a.理解方式1:可以将数组看成行和列构成,即理解成2行4 ...

  10. springboot打包,jar包可以以service方式启动

    <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> ...