题目地址:https://leetcode-cn.com/problems/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);

题目大意

设计一个电话目录管理系统,让它支持以下功能:

  • get: 分配给用户一个未被使用的电话号码,获取失败请返回 -1
  • check: 检查指定的电话号码是否可用
  • release: 释放掉一个电话号码,使其能够重新被分配

解题方法

数组

这个题比较简单,可以直接用最简单的做法,使用一个数组保存所有的数字是否被使用过。

  • get: 每次分配的时候从左向右依次遍历,找到第一个可用的数字并且把状态设置为已用。
  • check: 检查指定的电话号码是否可用
  • release:设置电话号码的状态为没有被使用过。

C++代码如下:

class PhoneDirectory {
public:
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
PhoneDirectory(int maxNumbers) {
used = vector<bool>(maxNumbers, false);
} /** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
int get() {
for (int i = 0; i < used.size(); ++i) {
if (!used[i]) {
used[i] = true;
return i;
}
}
return -1;
} /** Check if a number is available or not. */
bool check(int number) {
return !used[number];
} /** Recycle or release a number. */
void release(int number) {
used[number] = false;
}
private:
vector<bool> used;
}; /**
* 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);
*/

日期

2019 年 9 月 21 日 —— 莫生气,我若气病谁如意

最新文章

  1. 剑指Offer面试题:26.字符串的排列
  2. SSH整合 遇到的头疼的BUGs
  3. mysql源码解读之事务提交过程(一)
  4. Qt5 和 Qt4 的一些改动和不同
  5. bodyParser注意“需要请求头的支持”
  6. Qt之等待提示框(QPropertyAnimation)
  7. Ubuntu下GCC的安装以及版本控制
  8. java将Excel文件(xlsx,xls)转换为csv文件
  9. [Winfrom] 捕获窗体最大化、最小化和关闭按钮的事件
  10. (转) CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)
  11. IOS touch event animation 转动的风车
  12. php memcached缓存集群
  13. IMapControl3 Interface(1) Properties属性
  14. 前馈神经网络-反向传播(Back Propagation)公式推导走读
  15. ROS_Kinetic_x 基於ROS和Gazebo的RoboCup中型組仿真系統(多機器人協作)
  16. 跟随我在oracle学习php(9)
  17. 执行start-dfs.sh后,datenode没有启动的解决办法
  18. Android 开发自己的网络收音机4——读取XML文件的电台数据
  19. Git_添加远程库
  20. atcoder ARC092 D - Two Sequences 二分 &amp; 二进制

热门文章

  1. perl substr
  2. kafka的安装及使用
  3. Linux—软件包管理器yum安装详解
  4. python基础实战
  5. flink-----实时项目---day04-------1. 案例:统计点击、参与某个活动的人数和次数 2. 活动指标多维度统计(自定义redisSink)
  6. c#中实现串口通信的几种方法
  7. 使用Rapidxml重建xml树
  8. android studio 编译NDK android studio 生成.so文件
  9. ybatis中查询出多个以key,value的属性记录,封装成一个map返回的方法
  10. Output of C++ Program | Set 15