-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path380.o-1-时间插入、删除和获取随机元素.cpp
61 lines (49 loc) · 1.69 KB
/
380.o-1-时间插入、删除和获取随机元素.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
/*
* @lc app=leetcode.cn id=380 lang=cpp
*
* [380] O(1) 时间插入、删除和获取随机元素
*/
// @lc code=start
class RandomizedSet {
public:
// 用于存储对应的数据,我们直接 push_back 从末尾添加,
// 当我们需要删除一个数据的时候,我们只需要将其和尾部的数据进行交换即可,然后修改索引hash
// 所有的内容都是为了 getRandom 的时候可以在O(1)得到
// 因为当数组紧凑的时候我们可以直接生成随机数索引进行获取
vector<int> nums;
// 记录每个元素对应在 nums 的索引
unordered_map<int, int> val2index;
RandomizedSet() {
}
bool insert(int val) {
if (val2index.count(val)) return false;
val2index[val] = nums.size();
nums.push_back(val);
return true;
}
bool remove(int val) {
// 先把要删除的元素放至末尾,这样就可以直接 pop_back 即可
// 值得注意的是需要记得删除 map 中对应的 val index 索引
if (!val2index.count(val)) return false;
// 交换元素,并修改对应索引
int index = val2index[val];
val2index[nums.back()] = index;
int temp = nums[index];
nums[index] = nums.back();
nums.back() = val;
nums.pop_back();
val2index.erase(val);
return true;
}
int getRandom() {
return nums[rand()% nums.size()];
}
};
/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet* obj = new RandomizedSet();
* bool param_1 = obj->insert(val);
* bool param_2 = obj->remove(val);
* int param_3 = obj->getRandom();
*/
// @lc code=end