题目地址: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 日 —— 莫生气,我若气病谁如意

【LeetCode】379. Design Phone Directory 解题报告 (C++)的更多相关文章

  1. [LeetCode] 379. Design Phone Directory 设计电话目录

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

  2. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. 379. Design Phone Directory

    379. Design Phone Directory Design a Phone Directory which supports the following operations: get: P ...

  4. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  5. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  6. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  8. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

随机推荐

  1. 2015百度之星之-IP聚合

    IP聚合  Accepts: 138  Submissions: 293  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6553 ...

  2. mysql—mysql查询语句提示Unknown column ‘xxx’ in ‘where clause’

    运行结果中提示Unknown column 'xxx' in 'where clause'的问题.经过大神的指导,顿时明白其中缘由,如果sql中定义的类型是int型的可以不用加引号,但是如果是字符串类 ...

  3. javaSE高级篇3 — 网络编程 — 更新完毕

    网络编程基础知识 先来思考两个问题( 在这里先不解决 ) 如何准确的找到一台 或 多台主机? 找到之后如何进行通讯? 网络编程中的几个要素 IP 和 端口号 网络通讯协议:TCP / UDP 最后一句 ...

  4. LeetCode缺失的第一个正数

    LeetCode 缺失的第一个正数 题目描述 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数. 进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗? ...

  5. 27.0 linux VM虚拟机IP问题

    我的虚拟机是每次换一个不同的网络,b不同的ip,使用桥接模式就无法连接,就需要重新还原默认设置才行: 第一步:点击虚拟机中的编辑-->虚拟网络编辑器 第二步:点击更改设置以管理员权限进入 第三步 ...

  6. Spark Stage 的划分

    Spark作业调度 对RDD的操作分为transformation和action两类,真正的作业提交运行发生在action之后,调用action之后会将对原始输入数据的所有transformation ...

  7. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  8. sonic 安装记录

    https://github.com/valeriansaliou/sonic $ rustc --versionrustc 1.50.0-dev ubantu环境 rocksdb 安装依赖 apt ...

  9. java输入/输出流的基本知识

    通过流可以读写文件,流是一组有序列的数据序列,以先进先出方式发送信息的通道. 输入/输出流抽象类有两种:InputStream/OutputStream字节输入流和Reader/Writer字符输入流 ...

  10. MySQL-核心技术

    1.基本查询语句 1.1使用select 语句查询一个数据表 select * from user; 1.2 查询表中的一列或多列 select id,ixdh from user; 1.3从一个表或 ...