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 ...
随机推荐
- 小米oj 数组差(挺好的题)
数组差 序号:#46难度:困难时间限制:1000ms内存限制:10M 描述 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组元素和的差的绝对值 |SUM(A) - SUM(B)| 最大. ...
- Cogs 647. [Youdao2010] 有道搜索框(Trie树)
[Youdao2010] 有道搜索框 ★☆ 输入文件:youdao.in 输出文件:youdao.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在有道搜索框中,当输入一个或 ...
- python 生成螺旋矩阵
对于任意 m*n 矩阵,将 1~m*n 的数字按照螺旋规则在矩阵中排列. 如 m=3,n=3,期望结果为: [ [ , , ], [ , , ], [ , , ] ] 以下代码支持方阵以及非方阵. c ...
- Java中jdk代理和cglib代理
代理模式 给某一个对象提供一个代理,并由代理对象控制对原对象的引用.在一些情况下,一个客户不想或者不能够直接引用一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 在Java中代理模式从实 ...
- AD域渗透总结
域渗透总结 学习并做了一段时间域网络渗透,给我直观的感受就是思路问题和耐心,这个不像技术研究,需要对一个点进行研究,而是遇到问题后要从多个方面思考,寻找"捷径"思路,只要思路正确, ...
- 【零基础】快速入门爬虫框架HtmlUnit
迅速的HtmlUnit htmlunit是一款开源的web页面分析工具,理论上来说htmlunit应用于网页的自动化测试,但是相对来说更多人使用它来进行小型爬虫的快速开发.使用htmlunit进行爬虫 ...
- 微pe安装原版win10怎么装|wepe安装原版win10 1803教程
http://www.xitongcheng.com/jiaocheng/xtazjc_article_42199.html 怎么制作wepe启动盘?微pe是一款全新高效.多功能pe维护工具箱,同时支 ...
- 07 MySQL之视图
01-视图的含义 视图是从一个或者多个表中导出的,视图的行为与表非常相似,但视图是一个虚拟表.视图还可以从已经存在的视图的基础上定义. 02-创建视图 # 基本语法格式: CREATE [OR REP ...
- 使用Python爬取mobi格式电纸书
最近做了个微信推送kindle电子书的公众号:kindle免费书库 不过目前电子书不算非常多,所以需要使用爬虫来获取足够书籍. 于是,写了以下这个爬虫,来爬取kindle114的电子书. 值得注意的地 ...
- MySQL数据操作语句精解
用于操作数据库的SQL一般分为两种,一种是查询语句,也就是我们所说的SELECT语句,另外一种就是更新语句,也叫做数据操作语句. 言外之意,就是对数据进行修改.在标准的SQL中有3个语句,它们是INS ...