-
Notifications
You must be signed in to change notification settings - Fork 2
/
DesignPhoneDirectory.java
83 lines (66 loc) · 1.98 KB
/
DesignPhoneDirectory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package leetcode;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : DesignPhoneDirectory
* Creator : Edward
* Date : Jan, 2018
* Description : 379. Design Phone Directory
*/
public class DesignPhoneDirectory {
/**
* 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);
* @param maxNumbers
*/
HashSet<Integer> used = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
int maxNumbers;
public DesignPhoneDirectory(int maxNumbers) {
this.maxNumbers = maxNumbers;
for (int i = 0; i < maxNumbers; i++) {
queue.offer(i);
}
}
public int get() {
Integer res = queue.poll();
if (res == null) {
return -1;
}
used.add(res);
return res;
}
public boolean check(int number) {
if (number >= maxNumbers || number < 0) {
return false;
}
return !used.contains(number);
}
public void release(int number) {
if (used.remove(number)) {
queue.offer(number);
}
}
}