-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path379_Design Phone Directory.java
More file actions
46 lines (41 loc) · 1.26 KB
/
379_Design Phone Directory.java
File metadata and controls
46 lines (41 loc) · 1.26 KB
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
class PhoneDirectory {
boolean[] used;
Stack<Integer> stack;
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
public PhoneDirectory(int maxNumbers) {
used = new boolean[maxNumbers];
stack = new Stack<>();
for (int i = 0; i <maxNumbers; ++i) {
stack.push(i);
}
}
/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
public int get() {
if (stack.isEmpty()) {
return -1;
}
int num = stack.pop();
used[num] = true;
return num;
}
/** Check if a number is available or not. */
public boolean check(int number) {
return !used[number];
}
/** Recycle or release a number. */
public void release(int number) {
if (used[number]) {
stack.push(number);
used[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);
*/