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);

又是一道设计题,让我们设计一个电话目录管理系统,可以分配电话号码,查询某一个号码是否已经被使用,释放一个号码。既然要分配号码,肯定需要一个数组 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/

https://leetcode.com/problems/design-phone-directory/discuss/85328/Java-AC-solution-using-queue-and-set

https://leetcode.com/problems/design-phone-directory/discuss/122908/Java-O(1)-time-o(n)-space-single-Array-99ms-beats-100

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

[LeetCode] 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. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  3. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  4. [LeetCode] Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  5. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  6. Leetcode: Design Phone Directory

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

  7. [LeetCode] Design Linked List 设计链表

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

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

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

  9. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

随机推荐

  1. 读书笔记--SQL必知必会07--创建计算字段

    7.1 计算字段 字段(field),基本与列(column)含义相同. 利用计算字段可以直接从数据库中检索出转换.计算或格式化过的数据. 计算字段不实际存在于数据库表中,是运行时在SELECT语句内 ...

  2. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...

  3. PHP继承

    继承是PHP5面象对象程序设计的重要特性之一,它是指建立一个新的派生类,从一个或多个先前定义的类中继承数据和函数,而且可以重新定义或加进新数据和 函数,从而建立了类的层次或等级. 继承性是子类自动共享 ...

  4. HTML5 网络拓扑图整合 OpenLayers 实现 GIS 地图应用

    在前面<百度地图.ECharts整合HT for Web网络拓扑图应用>我们有介绍百度地图和 HT for Web 的整合,我们今天来谈谈 OpenLayers 和 HT for Web  ...

  5. 利用rebase来压缩多次提交

    我们可以用Git merge –squash来将分支中多次提交合并到master后,只保留一次提交历史.但是有些提交到github远程仓库中的commit信息如何合并呢? 历史记录 首先我们查看一下m ...

  6. 【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)

    前言:因项目需求,需要将HTML代码转成PDF.大致上已经实现了,可以是发现使用ITextSharp(我现在的版本是5.5.9)的时候,img标签中的src只能跟绝对路径. 在百度上找了一个上午,有一 ...

  7. 设计模式-代理模式(Proxy Model)

    文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/5988145.html 目录 1.写在前面 2.概述 3.目的 4.结构组成 5.实现 5 ...

  8. Hibernnate延迟加载策略(这么详细你还看不懂)

    好久没有认真写过博客了,今天就好好的写一篇吧!!!!!!!!! 当Hibernate 从数据库中加载某个对象(例如:Dept对象)时,如果同时自动加载所有的关联的某个对象(例如:Emp对象),而程序实 ...

  9. 10分钟写一个markdown编辑器

    marked.js Marked是一个Markdown解析引擎. vue.js Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vu ...

  10. session 存入数据库 php

     session 机制 1.php中session的生成机制 session是保存在服务器的,当我们在代码中调用session_start();时,PHP会同时往SESSION的存放目录(默认为/tm ...