-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path379.cpp
44 lines (40 loc) · 1.43 KB
/
379.cpp
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
__________________________________________________________________________________________________
class PhoneDirectory {
public:
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
PhoneDirectory(int maxNumbers) {
max_num = maxNumbers;
next = idx = 0;
recycle.resize(max_num);
flag.resize(max_num, 1);
}
/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
int get() {
if (next == max_num && idx <= 0) return -1;
if (idx > 0) {
int t = recycle[--idx];
flag[t] = 0;
return t;
}
flag[next] = false;
return next++;
}
/** Check if a number is available or not. */
bool check(int number) {
return number >= 0 && number < max_num && flag[number];
}
/** Recycle or release a number. */
void release(int number) {
if (number >= 0 && number < max_num && !flag[number]) {
recycle[idx++] = number;
flag[number] = 1;
}
}
private:
int max_num, next, idx;
vector<int> recycle, flag;
};
__________________________________________________________________________________________________
__________________________________________________________________________________________________