// 参考了下面一些讨论的解法
// https://discuss.leetcode.com/topic/53235/java-with-hashtable-arraylist/2 class RandomizedSet {
vector<int> vec;
unordered_map<int, int> umap;
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 (umap.find(val) != umap.end()) {
return false;
}
umap[val] = vec.size();
vec.push_back(val);
return true;
} /** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
if (umap.find(val) == umap.end()) {
return false;
}
int pos = umap[val];
int back = vec.back();
umap[back] = pos;
vec[pos] = back;
vec.erase(vec.begin()+vec.size()-);
umap.erase(val);
return true;
} /** Get a random element from the set. */
int getRandom() {
int randpos = rand() % vec.size();
return vec[randpos];
}
}; /**
* 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();
*/

insert-delete-getrandom-o1的更多相关文章

  1. [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  2. [LeetCode] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  3. LeetCode 380. Insert Delete GetRandom O(1)

    380. Insert Delete GetRandom O(1) Add to List Description Submission Solutions Total Accepted: 21771 ...

  4. 381. Insert Delete GetRandom O(1) - Duplicates allowed

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  5. [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  6. [LeetCode] 380. Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  7. [LeetCode] 380. Insert Delete GetRandom O(1) 插入删除获得随机数O(1)时间

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  8. [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 插入删除和获得随机数O(1)时间 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  9. LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed

    原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/?tab=Description ...

  10. leetcode 380. Insert Delete GetRandom O(1) 、381. Insert Delete GetRandom O(1) - Duplicates allowed

    380. Insert Delete GetRandom O(1) 实现插入.删除.获得随机数功能,且时间复杂度都在O(1).实际上在插入.删除两个功能中都包含了查找功能,当然查找也必须是O(1). ...

随机推荐

  1. jupyter notebook 小技巧

    Converting notebooks to other formats¶ !pip install https://github.com/ipython-contrib/jupyter_contr ...

  2. 绝对良心的 Java 中发邮件功能

    开篇语,是不是感觉这个功能都老掉牙了,网上一大推的文章,随便找个代码就是了,为什么我还要选择专门写一篇呢,因为我遇到了不一样的坑…… 首先,不免俗套的把代码都贴上来,拿去执行吧,记住换上你的账号和授权 ...

  3. python 与 mongodb的交互--更新操作

    这里只要讨论python与mongod交互的时候insert的问题: from pymongo import * def insert_func(): try: client_obj= MongoCl ...

  4. BZOJ 2726: [SDOI2012]任务安排 [斜率优化DP 二分 提前计算代价]

    2726: [SDOI2012]任务安排 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 868  Solved: 236[Submit][Status ...

  5. luoguP4503 [CTSC2014]企鹅QQ hash

    既然只有一位的不同,那么我们可以枚举这一位.... 我们只需要快速地计算去掉某一位的$hash$值.... 由于$hash(S) = \sum s[i] * seed^i$,因此去掉第$i$位的权值只 ...

  6. nginx_lua vs nginx+php 应用场景

    在我的印象中很多人还是选择nginx+php这种组合搭配,你的选择是nginx+lua,那么nginx+lua比和php的组合优势在哪里?清无:首先,Nginx+php之间是要有进程之间通信的,这样以 ...

  7. [HDU6155]Subsequence Count

    题目大意: 给定一个01序列,支持以下两种操作: 1.区间反转: 2.区间求不同的子序列数量. 思路: 首先我们考虑区间反转,这是一个经典的线段树操作. 接下来考虑求不同的子序列数量,在已知当前区间的 ...

  8. VC 操作 EXCEL---插入工作表(Insert.Sheet)方法

    看到的资料 http://bbs.csdn.net/topics/198565 自己总结一下 //插入到nIndex工作表之前 void InsertSheet(int nIndex) { sheet ...

  9. windows下配置ssh(FreeSSHD + putty)

    windows下配置ssh(FreeSSHD + putty): 1.关于配置过程找到一篇很好的博客,推荐大家先好好看一下,这篇博文解决了大方向问题. 地址:http://blog.csdn.net/ ...

  10. Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造

    E. Berland Local Positioning System Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...