标签:
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);
又是一道设计题,让我们设计一个电话目录管理系统,可以分配电话号码,查询某一个号码是否已经被使用,释放一个号码,需要注意的是,之前释放的号码下一次应该被优先分配。这题对C++解法的时间要求非常苛刻,尝试了好几种用set,或者stack/queue,或者使用vector的push_back等等,都TLE了,终于找到了一种可以通过OJ的解法。这里用两个一维数组recycle和flag,分别来保存被回收的号码和某个号码的使用状态,还有变量max_num表示最大数字,next表示下一个可以分配的数字,idx表示recycle数组中可以被重新分配的数字的位置,然后在get函数中,没法分配的情况是,当next等于max_num并且index小于等于0,此时返回-1。否则我们先看recycle里有没有数字,有的话先分配recycle里的数字,没有的话再分配next。记得更新相对应的flag中的使用状态,参见代码如下:
class PhoneDirectory { public: /** Initialize your data structure here @param maxNumbers - The maximum numbers that can be stored in the phone directory. */ PhoneDirectory(int maxNumbers) { max_num = maxNumbers; next = idx = 0; recycle.resize(max_num); flag.resize(max_num, 1); } /** Provide a number which is not assigned to anyone. @return - Return an available number. Return -1 if none is available. */ int get() { if (next == max_num && idx <= 0) return -1; if (idx > 0) { int t = recycle[--idx]; flag[t] = 0; return t; } flag[next] = false; return next++; } /** Check if a number is available or not. */ bool check(int number) { return number >= 0 && number < max_num && flag[number]; } /** Recycle or release a number. */ void release(int number) { if (number >= 0 && number < max_num && !flag[number]) { recycle[idx++] = number; flag[number] = 1; } } private: int max_num, next, idx; vector<int> recycle, flag; };
参考资料:
https://discuss.leetcode.com/topic/53136/all-c-solutions-got-lte/2
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Design Phone Directory 设计电话目录
标签:
原文地址:http://www.cnblogs.com/grandyang/p/5735205.html