[LC] 379. Design Phone Directory
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.
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的更多相关文章
- 379. Design Phone Directory
379. Design Phone Directory Design a Phone Directory which supports the following operations: get: P ...
- [LeetCode] 379. Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- 【LeetCode】379. Design Phone Directory 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组 日期 题目地址:https://leetcode ...
- [LeetCode] Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- Leetcode: Design Phone Directory
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- Design Phone Directory
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- LC 641. Design Circular Deque
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [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 ...
- [LC] 362. Design Hit Counter
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
随机推荐
- 面试准备 HTTP协议
http协议的主要特点 简单快速 //某个资源是固定的 (统一资源符)UII 灵活 //http头部有个数据类型,完成不同数据类型的传输 无连接 //链接一次就会断开 无状态 //客户端和服务端 ...
- Python—二叉树数据结构
二叉树 简介: 二叉树是每个结点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). 二叉树二叉树的链式存储: 将二叉树的节点定义为 ...
- 论 <解方程>
题面: 求n次整系数方程\(\sum_{i=1}^{n} a_ix^i = 0\)在区间\([1,m]\)上的整数解 解法: 1.暴力 O(NM) 暴力枚举+解方程 2.假设只要求一个解 瞎搞做法 引 ...
- Linux下常用的3种软件安装方式—rpm、yum、tar
一:Linux源码安装 1.解压源码包文件 源码包通常会使用tar工具归档然后使用gunzip或bzip2进行压缩,后缀格式会分别为.tar.gz与.tar.bz2,分别的解压方式: ...
- 转:以下是目前已经建立的sub一览 来自:https://zhuanlan.zhihu.com/p/91935757
转:以下是目前已经建立的sub一览 来自:https://zhuanlan.zhihu.com/p/91935757 作者: Lorgar 理工科 科学(和英文r/science一样,只接受论文讨论 ...
- Spring Cloud Alibaba 教程 | Nacos(三)
使用Nacos作为配置中心 前面我们已经介绍过滤Nacos是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台.所以它可以作为注册中心和配置中心,作为注册中心Nacos可以让我们灵活配置多 ...
- php 随机useragent
<?php /** * 获取随机useragent */ private function get_rand_useragent($param) { $arr = array( 'Mozilla ...
- 数据分析-Matplotlib:绘图和可视化
学习路线 简介 简单绘制线形图 plot函数 支持图类型 保存图表 1.简介 Matplotlib是一个强大的Python绘图和数据可视化的工具包.数据可视化也是我们数据分析的最重要的工作之一,可以帮 ...
- QeePHP
百度百科: https://baike.baidu.com/item/qeephp/8328612?fr=aladdin 官方地址: http://www.qeephp.cn/app/index.ph ...
- gcc -l:手动添加链接库
链接器把多个二进制的目标文件(object file)链接成一个单独的可执行文件.在链接过程中,它必须把符号(变量名.函数名等一些列标识符)用对应的数据的内存地址(变量地址.函数地址等)替代,以完成程 ...