-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
67 lines (58 loc) · 1.92 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class RandomizedSet {
private:
unordered_map<int, int> s;
vector<int> v;
public:
/** Initialize your data structure here. */
RandomizedSet() {
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val) {
if (s.count(val)) {
return false;
}
v.push_back(val);
s.insert({val, v.size() - 1});
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
if (!s.count(val)) {
return false;
}
int pos = s[val];
int last_val = v.back();
v[pos] = last_val;
v.pop_back();
s[last_val] = pos;
s.erase(val);
return true;
}
/** Get a random element from the set. */
int getRandom() {
std::srand(std::time(nullptr)); // use current time as seed for random generator
int pos = (int)(std::rand() * RAND_MAX) % v.size();
return v[pos];
}
};
int main() {
RandomizedSet randomizedSet;
cout << randomizedSet.insert(1) << endl; // Inserts 1 to the set. Returns true as 1 was inserted successfully.
cout << randomizedSet.remove(2) << endl; // Returns false as 2 does not exist in the set.
cout << randomizedSet.insert(2) << endl; // Inserts 2 to the set, returns true. Set now contains [1,2].
cout << randomizedSet.getRandom() << endl; // getRandom() should return either 1 or 2 randomly.
cout << randomizedSet.remove(1) << endl; // Removes 1 from the set, returns true. Set now contains [2].
cout << randomizedSet.insert(2) << endl; // 2 was already in the set, so return false.
cout << randomizedSet.getRandom() << endl; // Since 2 is the only number in the set, getRandom() will always return 2.
return 0;
}