[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 ...
随机推荐
- [ZJCTF 2019]NiZhuanSiWei
0x00知识点 1:data伪协议写入文件 2:php:// php://filter用于读取源码 php://input用于执行php代码 3反序列化 0x01解题 打开题目,给了我们源码 < ...
- 测网速 fping Linux查看网络即时网速 linux性能问题(CPU,内存,磁盘I/O,网络)
Linux查看网络即时网速 fping 是ping 工具的加强版本 例出局域网中存活的主机 (Ubuntu apt-get装上 cnetos装不上) zzx@zzx11:~$ fping -a 19 ...
- MyBatis从入门到精通(第9章):Spring集成MyBatis(上)
MyBatis从入门到精通(第9章):Spring集成MyBatis(上) Spring是一个为了解决企业级Web应用开发过程中面临的复杂性,而被创建的一个非常流行的轻量级框架. mybatis-sp ...
- UML-类图-箭头
概览 1.泛化 一般理解为 继承.实线+空心箭头 2.依赖 成员变量.局部变量.参数.虚线+箭头 public class Sale { public void updatePriceFor(Prod ...
- Java static 静态代码块、代码块
简述 static{} 静态代码块,加载类之前执行 {} 代码块,每次new的时候都会被执行 示例 类: public class Student { int age; String name; bo ...
- tensorflow实现卷积层的几种方式
#coding:utf-8 #第一种实现 tf.nn import tensorflow as tf import tensorflow.contrib.slim as slim tf.reset_d ...
- CodeForces 990B Micro-World(思维、STL)
http://codeforces.com/problemset/problem/990/B 题意: 有n个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且a ...
- map的嵌套 + 例题(水果)
水果 http://acm.hdu.edu.cn/showproblem.php?pid=1263 Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~Joe经营着一个 ...
- 7.windows-oracle实战第七课 --约束、索引
数据的完整性 数据的完整性用于确保数据库数据遵从一定的商业和逻辑规则.数据的完整性使用约束.触发器.函数的方法来实现.在这三个方法中,约束易于维护,具备最好的性能,所以作为首选. 约束:not nu ...
- mac词典安装
网上下载词典文件,比如https://www.xiaomac.com/2013121204.html,将朗道词典解压放入这里. $ ls /Library/Dictionaries/ langdao- ...