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 and so return True.directory.check (2);//It returns 2, the only number which is left.directory.get ();//The number 2 is no longer availa BLE, 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);
Another design problem, let us design a telephone directory management system, you can assign a phone number, query whether a number has been used, release a number, it should be noted that the number of previously released numbers should be assigned priority. This question on the C + + solution time requirements are very harsh, tried several kinds of set, or stack/queue, or use vector push_back and so on, all tle, and finally found a solution can be OJ. Here are two one-dimensional array recycle and flag, respectively, to save the recovered number and a number of the use of the State, as well as the variable max_num represents the largest number, next represents the next assignable number, IDX represents the position of the number of the recycle array can be reassigned, Then in the Get function, the inability to allocate is when next equals Max_num and index is less than or equal to 0, and returns-1 at this time. Otherwise we first look at the recycle there are no numbers, some words first allocated recycle in the number, no words redistribution next. Remember to update the usage status in the corresponding flag, see the code below:
classPhonedirectory { Public: /** Initialize Your data structure here @param maxnumbers-the maximum numbers the can is stored in the phone D Irectory. */Phonedirectory (intmaxnumbers) {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) { intt = recycle[--IDX]; Flag[t]=0; returnT; } Flag[next]=false; returnnext++; } /** Check If a number is available or not.*/ BOOLCheckintNumber ) { returnNumber >=0&& Number < Max_num &&Flag[number]; } /** Recycle or release a number.*/ voidReleaseintNumber ) { if(Number >=0&& number < Max_num &&!Flag[number]) {Recycle[idx++] =Number ; Flag[number]=1; } }Private: intmax_num, Next, IDX; Vector<int>recycle, Flag;};
Resources:
Https://discuss.leetcode.com/topic/53136/all-c-solutions-got-lte/2
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Design phone directory Designing telephone directories