Design a Phone Directory which supports the following operations:ide
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.code
// Init a phone directory containing a total of 3 numbers: 0, 1, and 2.
PhoneDirectory directory = new PhoneDirectory(3);rem
// It can return any available phone number. Here we assume it returns 0.
directory.get();get
// Assume it returns 1.
directory.get();it
// The number 2 is available, so return true.
directory.check(2);io
// It returns 2, the only number that is left.
directory.get();sed
// The number 2 is no longer available, so return false.
directory.check(2);List
// Release number 2 back to the pool.
directory.release(2);exception
// Number 2 is available again, return true.
directory.check(2);di
Set<Integer> used = new HashSet<Integer>(); Queue<Integer> available = new LinkedList<Integer>(); int max; public PhoneDirectory(int maxNumbers) { max = maxNumbers; for (int i = 0; i < maxNumbers; i++) { available.offer(i); } } public int get() { Integer ret = available.poll(); //Use Integer in case ret is null if (ret == null) { return -1; //Handle null exception } used.add(ret); return ret; } public boolean check(int number) { //Use HashSet only if (number >= max || number < 0) { return false; } return !used.contains(number); } public void release(int number) { //from used to available if (used.remove(number)) { available.offer(number); } }