[LeetCode] 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);
又是一道设计题,让我们设计一个电话目录管理系统,可以分配电话号码,查询某一个号码是否已经被使用,释放一个号码。既然要分配号码,肯定需要一个数组 nums 来存所有可以分配的号码,注意要初始化成不同的数字。然后再用一个长度相等的数组 used 来标记某个位置上的号码是否已经被使用过了,用一个变量 idx 表明当前分配到的位置。再 get 函数中,首先判断若 idx 小于0了,说明没有号码可以分配了,返回 -1。否则就取出 nums[idx],并且标记该号码已经使用了,注意 idx 还要自减1,返回之前取出的号码。对于 check 函数,直接在 used 函数中看对应值是否为0。最后实现 release 函数,若该号码没被使用过,直接 return;否则将 idx 自增1,再将该号码赋值给 nums[idx],然后在 used 中标记为0即可,参见代码如下:
解法一:
class PhoneDirectory {
public:
PhoneDirectory(int maxNumbers) {
nums.resize(maxNumbers);
used.resize(maxNumbers);
idx = maxNumbers - ;
iota(nums.begin(), nums.end(), );
}
int get() {
if (idx < ) return -;
int num = nums[idx--];
used[num] = ;
return num;
}
bool check(int number) {
return used[number] == ;
}
void release(int number) {
if (used[number] == ) return;
nums[++idx] = number;
used[number] = ;
}
private:
int idx;
vector<int> nums, used;
};
我们也可以使用队列 queue 和 HashSet 来做,整个思想和上面没有啥太大的区别,就是写法上略有不同,参见代码如下:
解法二:
class PhoneDirectory {
public:
PhoneDirectory(int maxNumbers) {
mx = maxNumbers;
for (int i = ; i < maxNumbers; ++i) q.push(i);
}
int get() {
if (q.empty()) return -;
int num = q.front(); q.pop();
used.insert(num);
return num;
}
bool check(int number) {
return !used.count(number);
}
void release(int number) {
if (!used.count(number)) return;
used.erase(number);
q.push(number);
}
private:
int mx;
queue<int> q;
unordered_set<int> used;
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/379
参考资料:
https://leetcode.com/problems/design-phone-directory/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Design Phone Directory 设计电话目录的更多相关文章
- [LeetCode] 379. Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- [LeetCode] Design Hit Counter 设计点击计数器
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- [LeetCode] Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- Leetcode: Design Phone Directory
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- [LeetCode] Design Linked List 设计链表
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- 【LeetCode】379. Design Phone Directory 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组 日期 题目地址:https://leetcode ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
随机推荐
- 【Python五篇慢慢弹】数据结构看python
数据结构看python 作者:白宁超 2016年10月9日14:04:47 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...
- php数据库访问及增删改
利用PHP访问由MySQL构建的数据库 连接到数据库 //1.造连接对象 $db = new MySQLi("localhost","root","1 ...
- 用Github pages搭建自己制作的网页,方法最简单,适用于新手
本文固定链接http://blog.csdn.net/pspgbhu/article/details/51205264 本人自学前端一个多月,写个几个网页想要用来应聘,网上搜各种搭建网站的方法,发现不 ...
- 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。
最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...
- github常见问题【转自百度知道】
1 git config --global user.name "Your Real Name" 2 git config --global user.email you@emai ...
- 本博客现已迁移到chuxiuhong.com
欢迎大家访问,我会暂时保留这个博客的更新,实现两个博客的同步. 新博客地址: http://chuxiuhong.com
- WCF备忘录一:服务端实例的生命周期
示例代码下载地址:WCFDemo1Day 概述 客户端向WCF服务发出请求后,服务端会实例化一个Service对象(实现了契约接口的对象)用来处理请求,实例化Service对象以及维护其生命周期的方式 ...
- 初识Hadoop
第一部分: 初识Hadoop 一. 谁说大象不能跳舞 业务数据越来越多,用关系型数据库来存储和处理数据越来越感觉吃力,一个查询或者一个导出,要执行很长 ...
- LINQ to SQL语句(14)之Null语义和DateTime
Null语义 说明:下面第一个例子说明查询ReportsToEmployee为null的雇员.第二个例子使用Nullable<T>.HasValue查询雇员,其结果与第一个例子相同.在第三 ...
- Idea SpringMVC+Spring+MyBatis+Maven调整【转】
Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...