题目链接: https://leetcode.com/problems/design-phone-directory/

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

思路: 这题的对C++时间卡的太严了, 基本上只要涉及到任何高级数据结构就会超时, 只能利用c风格的代码, 并且所有操作除初始化以外都是O(1)才可以过.

可以利用两个数组和一个当前剩余号码计数, 一个数组前n个保存当前可用的号码, 另一个数组映射每一个号码是否可用, 还有一个计数是当前第一个数组前n个是可用的.

代码如下:

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

[leetcode] 379. Design Phone Directory 解题报告相关推荐

  1. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  2. [LeetCode]844. Backspace String Compare 解题报告(C++)

    [LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...

  3. 【LeetCode】91. Decode Ways 解题报告(Python)

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

  4. Leetcode 第133场周赛解题报告

    今天参加了leetcode的周赛,算法比赛,要求速度比较快.有思路就立马启动,不会纠结是否有更好的方法或代码可读性.只要在算法复杂度数量级内,基本上是怎么实现快速就怎么来了. 比赛时先看的第二题,一看 ...

  5. 【LeetCode】934. Shortest Bridge 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...

  6. 【LeetCode】127. Word Ladder 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...

  7. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  8. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

  9. 【leetcode 968. 监控二叉树】解题报告

    解题思路: 由于叶子节点一定不要安装监视器,这样才能使总监视器数量比较少,因此需要从下往上进行判断当前节点的状态(共:3种状态): 0: 当前节点安装了监视器 1: 当前节点可观,但没有安装监视器 2 ...

  10. 【LeetCode】517. 超级洗衣机 解题报告 (python)

    原题地址:https://leetcode-cn.com/problems/super-washing-machines/submissions/ 题目描述: 假设有 n 台超级洗衣机放在同一排上.开 ...

最新文章

  1. python3爬虫实例-python3.7简单的爬虫实例详解
  2. Linux自定义命令
  3. “is”与“==”区别
  4. [转]用Whois获得电信运营商的IP地址是如何分配的?
  5. 解决由于sz rz导致抓包时文件容量增加
  6. linux+性能排查,Linux系统性能排查基础
  7. 瑞利衰落(Rayleigh Fading)
  8. 发票管理软件_企业为什么需要采购管理软件?
  9. 关于DB9和DB25
  10. vivado中bit文件怎么没有生成_【新手入门】ISE工程升级到Vivado及板级信号调试技术...
  11. MPEG4写为avi文件
  12. html怎么快捷复制粘贴,如何使用快捷键复制粘贴
  13. 轻松调整C盘分区大小
  14. AngularJS PrimeNG 上传文件 进度条
  15. 第6章 系统数据文件和信息
  16. Promise面试题汇总
  17. AR增强现实的三大关键技术
  18. 儿童编程 python培训
  19. 移动互联网十年(建议收藏)
  20. c语言打印如来佛字符,JS控制台打印如来佛加持护身符

热门文章

  1. SpringCloud 之分布式 CAP 定理
  2. 台式计算机无线网络,台式电脑如何使用无线上网?
  3. 数据结构——栈的详解
  4. 红帽linux安装网卡,redhat网卡驱动程序安装步骤
  5. Iptables入门
  6. javax.validation校验整理
  7. 微信公众平台测试账号申请地址
  8. 小学班级计算机社团活动章程,西华小学速算社团活动章程.doc
  9. JavaScript实现邮箱后缀提示功能
  10. 《她身之欲》(珠三角阅流动人口社群特殊职业研究)阅读感想