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.
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的更多相关文章
- 379. Design Phone Directory
379. Design Phone Directory Design a Phone Directory which supports the following operations: get: P ...
- [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 ...
- [LeetCode] 379. Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- [LC] 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(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
随机推荐
- 013_STM32程序移植之_DS18B20
013_STM32程序移植之_DS18B20 1. 测试环境:STM32C8T6 2. 测试模块:DS18B20模块 3. 测试接口: 1. DS18B20模块接口: DS18B20引脚 ...
- 百度智能api接口汇总
一:自然语言处理 # -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/18 9:47 # pip install baidu-aip ...
- 记录二:tensorflow2.0写MNIST手写体
最近学习神经网络,tensorflow,看了好多视频,查找了好多资料,感觉东西都没有融入自己的思维中.今天用tensorflow2.0写了一个MNIST手写体的版本,记录下学习的过程. 复现手写体识别 ...
- 五一培训 清北学堂 DAY4
今天上午是钟皓曦老师的讲授,下午是吴耀轩老师出的题给我们NOIP模拟考了一下下(悲催暴零) 今天的内容——数论 话说我们可能真的是交了冤枉钱了,和上次清明培训的时候的课件及内容一样(哭. 整除性 质数 ...
- Tomcat7修改根路径应用
大家想必遇到过这样的问题,同台机器上跑上多个tomcat 那么随之更改文件的工作量就会增加 今天突然想到把所有的tomcat的根目录更改成一个文件 但是有不好的就的地方就是在更改文件的时候需要把这台机 ...
- python itern机制的
这些变量很可能在许多程序中使用. 通过池化这些对象,Python可以防止对一致使用的对象进行内存分配调用. 1.介于数字-5和256之间的整数 2.字符串仅包含字母.数字或下划线
- MySQL单机安装
操作系统:CentOS 7 MySQL:5.6 MySQL的卸载 查看MySQL软件 卸载MySQL 查看是否还有 MySQL 软件,有的话继续删除. 安装MySQL 启动MySQL 设置root用户 ...
- div设置百分比高度 宽度
给div按百分比设置高度 宽度两种方法: 第一种是给body标签设置他的高度值,xxxpx,div就会根据body的像素值取百分比: 第二种方法就是在div属性中加入 position:absolut ...
- shell 基数数值方法
shell 下获取数值的结果 1. # expr 1 "+" 2 2. # echo "1+2" |bc 3. # echo $(( 1+3))
- LC 722. Remove Comments
Given a C++ program, remove comments from it. The program source is an array where source[i] is the ...