379. Design Phone Directory
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 {
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的更多相关文章
- [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 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 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
随机推荐
- DFA敏感词过滤
import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.ArrayList; ...
- VMware Workstation 10+Centos7(64位)共享文件夹
这一两天一直在研究VMware Workstation自带的共享文件夹的功能,期间出了不少问题,在公司搭建的是vm10.0+centos07,在家搭建的是VM 7+centos07... 公司环境搭建 ...
- .net乱码问题
最近在给一个客户做framwwork版本升级,从1.0版本升级到4.0版本,发现最大的问题就是乱码. 在1.0版本下,gb2312编码能够运行得很好,可是升级到4.0后就都是乱码. 随后将webcon ...
- 设置apache登陆密码验证
一.编辑虚拟目录配置文件,设置认证方式 Alias /test"/var/www/test"<Directory "/var/www/test">O ...
- C——数组下标与间址运算符
只说一句,数组下标与间址运算符*是等价的,即:a[i] = *(a+i),看代码: int main(int argc, char* argv[]) { ] = {, , , , }; int i; ...
- tomcat安全加固
-R 640 conf/*3. 首次安装完成后立即删除webapps下面的所有代码rm -rf /srv/apache-tomcat/webapps/*4. 注释或删除 tomcat-users.xm ...
- 四核exynos4412开发板使用网线上网注意事项
问:RP4412开发板板子可以插网线上网? 答:可以.支持WIFI.LAN.3/4G上网的.插网线没? 问:我插了,他还是提示让我连wifi. 答:你是上网页还是其他的APP. 网页可以直接打开,有部 ...
- 小游戏Step表
[Config]1|0|0|2|172,120,134|公主的记忆|1|2[Config] [Config]2|1|1|2|172,120,134|公主的记忆|1|2[Config] [Config] ...
- tweenmax.js 文档
TweenMax 参考http://bbs.9ria.com/thread-214959-1-1.html TweenMax 可能是很多人都用的,包括我 但 是最近发现大量的运用就总会产生这样或那样的 ...
- 在MyEclipse中配置Tomcat服务器
http://wenku.baidu.com/link?url=j0rrOEvt10zgsJVWX3wuV26uVc2fz5MhIusvLWzvK_KZWMKq60wuDaMnHGck0PR6sCrU ...