【LeetCode】379. Design Phone Directory 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址: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
: 分配给用户一个未被使用的电话号码,获取失败请返回 -1check
: 检查指定的电话号码是否可用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++)的更多相关文章
- [LeetCode] 379. Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 379. Design Phone Directory
379. Design Phone Directory Design a Phone Directory which supports the following operations: get: P ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- C++ and OO Num. Comp. Sci. Eng. - Part 3.
2. Expressions and Statements 声明是将一个种类型的变量引入程序的语句. 作用域 作用域又一对花括号限定,在所有花括号之外的为全局作用域. 在作用域内声明的变量为局部变量. ...
- SNPEFF snp注释 (添加自己基因组)
之间介绍过annovar进行对snp注释,今天介绍snpEFF SnpEff is a variant annotation and effect prediction tool. It annota ...
- 【3】蛋白鉴定软件之Mascot
目录 1.简介 2.配置 2.1在线版本 2.2 服务器版本 3.运行 3.1 在线版本 3.2 服务器版本 4.结果 1.简介 Mascot是非常经典的蛋白鉴定软件,被Frost & Sul ...
- python 新闻管理系统——启示
mysql取整函数: mysql函数ceil.floor.round mysql 取整 1.ceil() / ceiling() 向上取整 ex: ceil(1.2) = 2 2.floor() 向下 ...
- react native安装遇到的问题
最近学习react native 是在为全栈工程师而努力,看网上把react native说的各种好,忍不住学习了一把.总体感觉还可以,特别是可以开发android和ios这点非常厉害,刚开始入门需要 ...
- 轻松理解webpack热更新原理
一.前言 - webpack热更新 Hot Module Replacement,简称HMR,无需完全刷新整个页面的同时,更新模块.HMR的好处,在日常开发工作中体会颇深:节省宝贵的开发时间.提升开发 ...
- oracle 日期语言格式化
TO_DATE ('17-JUN-87', 'dd-mm-yy', 'NLS_DATE_LANGUAGE = American')
- Linux学习 - fdisk分区
一.fdisk命令分区过程 系统一旦重启,分区将消失 1 添加新硬盘 直接在虚拟机上添加 2 查看新硬盘 fdisk -l 3 分区 fdisk /dev/sdb fdisk进入/dev/sdb硬件设 ...
- PhoneGap本地将html打包成安卓App
PhoneGap的在线打包有大小限制,超过30M的包无法在线打包.当然,可以把包里面的图片.声音文件去掉,然后打包.下载以后,解包,重新打包并签名.蛮麻烦的. 本地打包的简单方法如下: 下载安装Jav ...
- spring boot druid数据源
pom.xml配置 <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <art ...