[LeetCode] 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);
又是一道设计题,让我们设计一个电话目录管理系统,可以分配电话号码,查询某一个号码是否已经被使用,释放一个号码。既然要分配号码,肯定需要一个数组 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] 379. Design Phone Directory 设计电话目录的更多相关文章
- [LeetCode] Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- 379. Design Phone Directory
379. Design Phone Directory Design a Phone Directory which supports the following operations: get: P ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- 【LeetCode】379. Design Phone Directory 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组 日期 题目地址:https://leetcode ...
- [LeetCode] 362. Design Hit Counter 设计点击计数器
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- LeetCode 707. Design Linked List (设计链表)
题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...
- [LC] 379. Design Phone Directory
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
随机推荐
- argparse.ArgumentParser()用法解析
本博客主要本人学习记录用. 内容来源 于博客:https://blog.csdn.net/the_time_runner/article/details/97941409 argparse模块官方文档 ...
- MyCat(转)
https://www.cnblogs.com/bingosblog/p/7171501.html http://www.cnblogs.com/joylee/p/7513038.html ht ...
- JVM的内存分配策略
1.对象优先在Eden区分配大多数情况下,对象在新生代Eden区中分配.当Eden区没有足够空间进行分配时,虚拟机将发起一次Minor GC. 2.大对象直接进入老年代 所谓的大对象是指,需要大量连续 ...
- ML.NET调用Tensorflow模型示例——MNIST
ML.NET在不久前发行了1.0版本,在考虑这一新轮子的实际用途时,最先想到的是其能否调用已有的模型,特别是最被广泛使用的Tensorflow模型.于是在查找了不少资料后,有了本篇示例.希望可以有抛砖 ...
- 关于5G手机使用4G套餐扫盲
有些人说换5G手机用4G套餐不用5G套餐可以享受最高 300 mbps 的签约速率.在此我来给你们科普下. 5G套餐分为 500 mbps 和 1000 mbps 两种.且都享受优先接入,顺序是 10 ...
- C# 学习笔记 多态(一)虚方法
在面对对象编程中,类的三大特性分别为封装,继承,多态.其中多态的具体实现,依赖于三个方法,也就是虚方法,抽象类和接口. 多态的具体作用是什么呢?或者说多态的存在有什么意义呢?多态的存在有效的降低了程序 ...
- 十:装饰器模式(io流)
定义:装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 这一个解释,引自百度百科,我们 ...
- echarts白色实心环形图(空心饼图)的编写
// 数据接入机构统计let myDom = document.getElementById('myChart');let myWidth = myDom.offsetWidth - 5; // 获取 ...
- ANDROID培训准备资料之项目结构简单介绍
Android Studio项目结构初步主要介绍下面几个文件夹,后续再补充 (1)java文件夹的介绍 (2)Res文件夹的介绍 (3)R文件的介绍 (4)Manifests文件夹的介绍 我们先看看整 ...
- Objective-C学习——中文URL编码和解码
发现NSString类中有内置的方法可以实现.他们分别是: - (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncod ...