### 题目

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


### 直观解：数字在2个集合中倒来倒去，复杂度O(n)

class PhoneDirectory {
public PhoneDirectory(int maxNumbers) {
max = maxNumbers;
for (int i = 0; i < maxNumbers; i++) {
available.offer(i);
}
}

public int get() {
Integer ret = available.poll();
if (ret == null) {
return -1;
}
return ret;
}

public boolean check(int number) {
if (number >= max || number < 0) {
return false;
}
return !used.contains(number);
}

public void release(int number) {
if (used.remove(number)) {
available.offer(number);
}
}

private Set<Integer> used = new HashSet<Integer>();
private Queue<Integer> available = new LinkedList<Integer>();
private int max;
}


### 用一个HashSet回收池，复杂度O(1)

#### 代码

class PhoneDirectory {

/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
public PhoneDirectory(int maxNumbers) {
max = maxNumbers;
bankP = 0; // point to next number to assign
recycle = new HashSet<Integer>();
}

/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
public int get() {
if (!recycle.isEmpty()) {
Iterator<Integer> ite = recycle.iterator();
int num = ite.next();
ite.remove();
return num;
}
if (bankP < max) {
return bankP++;
}
return -1;
}

/** Check if a number is available or not. */
public boolean check(int number) {
return legal(number) && !assigned(number);
}

/** Recycle or release a number. */
public void release(int number) {
if (legal(number) && assigned(number)) {
}
}

/**====================== 【私有成员】 ========================*/
private int max;
private int bankP;
private Set<Integer> recycle;

private boolean legal(int number) {
return number >= 0 && number < max;
}
private boolean assigned(int number) {
return number < bankP && !recycle.contains(number);
}
}


### 单一数组解

1. 每个槽位的下标代表数字
2. 数组桶内的值代表这个数字的下一个元素的偏移值

下标[0]代表这个数字的值是0，桶内值为[1]代表这个数的下一个元素的偏移值为1。
+---+
|   |
[1,  2,  3,  4,  5,  6,  7,  8,  9,  0]
|                                   |
+-----------------------------------+
下标[9]代表这个数字的值是9，桶内值为[0]代表这个数的下一个元素的偏移值为0。


get()get()release(0)之后的数组，如下图所示，

#### 代码

class PhoneDirectory {

public PhoneDirectory(int maxNumbers) {
for (int i = 0; i < maxNumbers; i++) {
linkedTable[i] = (i + 1) % maxNumbers;
}
p = 0;
}

public int get() {
return -1;
}
int nextNum = p;
return nextNum;
}

public boolean check(int number) {
}

public void release(int number) {
if (!check(number)) {
p = number;
}
}

/**====================== 【私有成员】 ========================*/
private int p;

}


### 用BitSet，O(n)

#### 代码

class PhoneDirectory {

public PhoneDirectory(int maxNumbers) {
set = new BitSet();
smallestFreeBit = 0;
max = maxNumbers;
}

public int get() {
if (smallestFreeBit == max) {
return -1;
}
int res = smallestFreeBit;
set.set(smallestFreeBit);
smallestFreeBit = set.nextClearBit(smallestFreeBit);
return res;
}

public boolean check(int number) {
return !set.get(number);
}

public void release(int number) {
if (set.get(number)) {
set.clear(number);
if (number < smallestFreeBit) {
smallestFreeBit = number;
}
}
}

/**====================== 【私有成员】 ========================*/
BitSet set;
int smallestFreeBit = 0;
int max;

}