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 ...
随机推荐
- Hibernate 4 升级到 Hibernate 5 的时候 SessionFactory 不能使用
在 Hibernate 4 升级到 5 的时候老的 sessionFactory 出现错误. public static SessionFactory initSession() { Configur ...
- Splay - restudy
https://www.zybuluo.com/wsndy-xx/note/1136246 图1 图2
- 「BZOJ 2653」middle「主席树」「二分」
题意 一个长度为\(n\)的序列\(a\),设其排过序之后为\(b\),其中位数定义为\(b[n/2]\),其中\(a,b\)从\(0\)开始标号,除法取下整.给你一个长度为\(n\)的序列\(s\) ...
- 人脸检测之Haar-like,Adaboost,级联(cascade)
最新版本整理完毕,见: http://face2ai.com/MachineLearning-Haar-like-Adaboost-cascade 0:写在前面的话 写在前面的牢骚 ...
- codeforces997C
Sky Full of Stars CodeForces - 997C On one of the planets of Solar system, in Atmosphere University, ...
- Hdu 5884
hdu 5884 Sort 题意: n个有序序列的归并排序.每次可以选择不超过k个序列进行合并,合并代价为这些序列的长度和,总的合并代价不能超过T, 问k最小是多少. 解法: 1:首先想到的是二分这个 ...
- vuecli3.0 webpack4 配置vuex
废话不说,直接写步骤 1. npm install vux --save 2. npm install less less-loader --save-dev 3. npm install @vux/ ...
- Ubuntu桌面版与服务器版的区别(转)
Ubuntu桌面版vs服务器版 提到安装Linux,Ubuntu可谓是最受欢迎的.为了满足每个人的需求,出现了不少版本或风格的Ubuntu:其中两项便是桌面版与服务器版.只要发布版本号一致,这两者从核 ...
- 3.ibatis4种事务类型浅析
ibatis中Transaction有四个实现类 其中spring的SqlMapClientFactoryBean类中 private Class transactionConfigClass = E ...
- mysql授权指定ip远程登录
use user //更新用户表: UPDATE `user` SET `Host` = '175.6.6.230' where `Host` = '175.6.6.230'; //授权用户表: GR ...