379. Design Phone Directory

Design a Phone Directory which supports the following operations:

  1. get: Provide a number which is not assigned to anyone.
  2. check: Check if a number is available or not.
  3. 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 {
public:
vector<int> nums;
vector<bool> flag;
int pos;
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
PhoneDirectory(int maxNumbers) {
pos = maxNumbers;
nums.resize(maxNumbers);
flag.resize(maxNumbers);
for (int i = ; i < maxNumbers; i++) {
nums[i] = i;
flag[i] = true;
}
} /** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
int get() {
if (pos > ) {
int cur = nums[--pos];
flag[cur] = false;
return cur;
}
return -;
} /** Check if a number is available or not. */
bool check(int number) {
return flag[number];
} /** Recycle or release a number. */
void release(int number) {
if (!flag[number]) {
flag[number] = true;
nums[pos++] = number;
}
}
}; /**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj.get();
* bool param_2 = obj.check(number);
* obj.release(number);
*/

379. Design Phone Directory的更多相关文章

  1. [LeetCode] 379. Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  2. [LC] 379. Design Phone Directory

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  3. 【LeetCode】379. Design Phone Directory 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组 日期 题目地址:https://leetcode ...

  4. [LeetCode] Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  5. Leetcode: Design Phone Directory

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  6. Design Phone Directory

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

随机推荐

  1. 理解HTTP和HTTPS的区别

    原问转载于https://www.mysubmail.com/chs/blog/view/47 这两天闲来无事,在网上看了一下,发现 HTTP 和 HTTPS 的区别很受关注,多位大牛做了很详细的阐述 ...

  2. Gradle 使用本地的Jar包(gradle oracle ojdbc14 )

    Gradle 使用本地的Jar包(gradle oracle ojdbc14 ) 因为Oracle的驱动包在Maven上是没办法直接下载到的,所以在使用Gradle的使用,会导致无法加载Oracle, ...

  3. systemctl命令用法详解

    systemctl命令用法详解系统环境:Fedora 16binpath:/bin/systemctlpackage:systemd-units systemctl enable httpd.serv ...

  4. SQLite3中自增主键归零方法

    当SQLite数据库中包含自增列时,会自动建立一个名为 sqlite_sequence 的表.这个表包含两个列:name和seq.name记录自增列所在的表,seq记录当前序号(下一条记录的编号就是当 ...

  5. JavaScipt 数据交互

    标准的w3c直接提供了XMLHttpRequest方法,我们主要站在设计的角度来理解,如何设计出低耦合高内聚的代码jquery对Ajax的处理主要体现在对浏览器兼容,数据的处理过滤以及各种事件的封装上 ...

  6. JavaScript模块化

    1.commonjs 导入: var math = require('math'); math.add(2,3); // 5 导出: module.exports={} 应用会停止并等待加载 2.AM ...

  7. python 函数传递参数的多种方法

    python中函数根据是否有返回值可以分为四种:无参数无返回值,无参数有返回值,有参数无返回值,有参数有返回值. Python中函数传递参数的形式主要有以下五种,分别为位置传递,关键字传递,默认值传递 ...

  8. JSP自定义标签/自定义标签打包

    有这样一个业务需求: 当我们在编辑某个用户时,需要设置该用户的角色,在转到编辑页面时,就需要自动勾选上该用户已经选择的角色,如下图: 当我们点击编辑时,会查询用户详细信息,以及角色集合传到编辑页面. ...

  9. BZOJ3000 Big Number

    由Stirling公式: $$n! \approx \sqrt{2 \pi n} (\frac{n}{e})^n$$ 故:$$\begin{align} ans &= log_k n! + 1 ...

  10. nodeType的意思

    nodeType是用来获得当前节点对象的类型.nodeType 属性可返回节点的类型.元素element   1   属性attr   2   文本text   3   注释comments   8 ...