-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetCode-379-Design-Phone-Directory.java
55 lines (47 loc) · 1.25 KB
/
LeetCode-379-Design-Phone-Directory.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
class PhoneDirectory {
int[] nums;
int count;
int i;
public PhoneDirectory(int maxNumbers) {
this.nums = new int[maxNumbers];
this.count = 0;
this.i = 0;
Arrays.fill(nums, -1);
}
public int get() {
if (count == nums.length) return -1;
if (this.i == -1) return -1;
int available = i;
nums[i] = i;
count++;
this.i = -1;
// find next available
if (count < nums.length) {
for (int k = 0; k < nums.length; k++) {
if (k == this.i) continue;
if (nums[k] == -1) {
this.i = k;
break;
}
}
}
return available;
}
public boolean check(int number) {
return nums[number] == -1;
}
public void release(int number) {
if (nums[number] == number) {
nums[number] = -1;
count--;
}
this.i = number;
}
}
/**
* 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);
*/