-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
40 lines (34 loc) · 1.11 KB
/
Solution.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
class PhoneDirectory {
private boolean[] provided;
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
public PhoneDirectory(int maxNumbers) {
provided = new boolean[maxNumbers];
}
/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
public int get() {
for (int i = 0; i < provided.length; ++i) {
if (!provided[i]) {
provided[i] = true;
return i;
}
}
return -1;
}
/** Check if a number is available or not. */
public boolean check(int number) {
return !provided[number];
}
/** Recycle or release a number. */
public void release(int number) {
provided[number] = false;
}
}
/**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj.get();
* boolean param_2 = obj.check(number);
* obj.release(number);
*/