【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/ 题目地址 ...
随机推荐
- 拒绝恶意同构ssh登陆服务器脚本
#!/bin/bash #Deny specified IP access #IP:who is fail to login sever SECURE_LOG=/var/log/secure #通过s ...
- lsof之列出已打开的文件
lsof命令常用解析 Linux中常用 lsof 来查看文件调用进程等相关信息,也可用来查看活跃的进程信息和端口监听进程信息等 1. lsof 命令介绍 NAME lsof - list open f ...
- mysql 索引的注意事项
mysql 无法使用索引的查询 索引是什么,为什么要用索引,索引使用的时候要注意什么,那些情况下索引无法起作用. 1,索引是什么 mysql的索引也是一张表,并且是一个有序的表,主要记录了需要索引的数 ...
- day10设置文件权限
day10设置文件权限 yum复习 1.修改IP [root@localhost ~]# sed -i 's#.200#.50#g' /etc/sysconfig/network-scripts/if ...
- 乱序拼图验证的识别并还原-puzzle-captcha
一.前言 乱序拼图验证是一种较少见的验证码防御,市面上更多的是拖动滑块,被完美攻克的有不少,都在行为轨迹上下足了功夫,本文不讨论轨迹模拟范畴,就只针对拼图还原进行研究. 找一个市面比较普及的顶像乱序拼 ...
- 分配器——allocators
任何容器的构建都离不开分配器,分配器顾名思义就是分割配置内存资源的组件,分配器的效率直接影响力容器的效率. operator new()和malloc() C/C++底层都是通过malloc()调用系 ...
- 创建Oracle数据库实例
创建Oracle数据库实例 转自oracle数据库创建实例 数据库已经安装完成,可以正常登陆查看用户等操作. system用户只能用normal身份登陆em.除非你对它授予了sysdba的系统权限或者 ...
- winxp 关闭445端口
关闭445端口的方法方法很多,但是我比较推荐以下这种方法: 修改注册表,添加一个键值 Hive: HKEY_LOCAL_MACHINE Key: System\Controlset\Services\ ...
- d3基础入门一-选集、数据绑定等核心概念
引入D3 D3下载,本文下载时的版本为6.5.0 mkdir d3.6.5.0 unzip --help unzip d3.zip -d d3.6.5.0/ ls d3.6.5.0/ API.md C ...
- 使用NSURLSessionDownloadTask实现大文件下载-监听下载进度
- 5.1 涉及知识点(1)创建NSURLSession并设置代理,通过NSURLSessionDownloadTask并以代理的方式来完成大文件的下载 //1.创建NSURLSession,设置代理 ...