-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
76 lines (68 loc) · 1.87 KB
/
Solution.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
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
struct Node {
int val;
vector<Node*> next;
Node(int v, int level)
: val(v)
, next(level, nullptr) {}
};
class Skiplist {
public:
const int p = RAND_MAX / 4;
const int maxLevel = 32;
Node* head;
int level;
Skiplist() {
head = new Node(-1, maxLevel);
level = 0;
}
bool search(int target) {
Node* curr = head;
for (int i = level - 1; ~i; --i) {
curr = findClosest(curr, i, target);
if (curr->next[i] && curr->next[i]->val == target) return true;
}
return false;
}
void add(int num) {
Node* curr = head;
int lv = randomLevel();
Node* node = new Node(num, lv);
level = max(level, lv);
for (int i = level - 1; ~i; --i) {
curr = findClosest(curr, i, num);
if (i < lv) {
node->next[i] = curr->next[i];
curr->next[i] = node;
}
}
}
bool erase(int num) {
Node* curr = head;
bool ok = false;
for (int i = level - 1; ~i; --i) {
curr = findClosest(curr, i, num);
if (curr->next[i] && curr->next[i]->val == num) {
curr->next[i] = curr->next[i]->next[i];
ok = true;
}
}
while (level > 1 && !head->next[level - 1]) --level;
return ok;
}
Node* findClosest(Node* curr, int level, int target) {
while (curr->next[level] && curr->next[level]->val < target) curr = curr->next[level];
return curr;
}
int randomLevel() {
int lv = 1;
while (lv < maxLevel && rand() < p) ++lv;
return lv;
}
};
/**
* Your Skiplist object will be instantiated and called as such:
* Skiplist* obj = new Skiplist();
* bool param_1 = obj->search(target);
* obj->add(num);
* bool param_3 = obj->erase(num);
*/