// 参考了下面一些讨论的解法
// 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. (转)最短路算法 -- Floyd算法

    转自:http://blog.51cto.com/ahalei/1383613        暑假,小哼准备去一些城市旅游.有些城市之间有公路,有些城市之间则没有,如下图.为了节省经费以及方便计划旅程 ...

  2. ref:Mysql授权远程登陆

    ref:https://blog.csdn.net/qq_26710805/article/details/79776897 在Windows环境上操作.步骤如下: 1. 打开cmd窗口,登陆mysq ...

  3. QT防止程序启动两次的方法

    为了使QT 能保证只创建一个实例来进行, 对windows和linux分别采取了全局互斥变量和文件锁的方法. Q_OS_WIN32宏用来表示编译运行的目标平台是windows,Q_OS_LINUX则标 ...

  4. Chrome谷歌浏览器拓展组件的2种快速安装方法(.crx)

    谷歌浏览器拓展有至少2种安装方法,现在简单的介绍下. 第一种.当然是进入谷歌官方的应用商店直接安装 这种方法简单快捷,而且官方支持度够高,唯一的缺点是大陆用户需要“FQ”. 谷歌拓展组件应用商店地址: ...

  5. TCP的三次握手与四次释放

    TCP的三次握手与四次释放 一.名词解释     序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序号,第一个字节的编号由本地随机产生:给字节编上序号后,就给 ...

  6. hdu 3864 素数分解

    题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...

  7. 【ACM-ICPC 2018 沈阳赛区网络预赛】不太敢自称官方的出题人题解

    A. Gudako and Ritsuka 链接 by Yuki & Asm.Def 期望难度:Hard- 考虑从后往前进行博弈动态规划,在这一过程中维护所有的先手必胜区间.区间不妨采用左开右 ...

  8. 【JavaScript代码实现三】JS对象的深度克隆

    function clone(Obj) { var buf; if (Obj instanceof Array) { buf = []; // 创建一个空的数组 var i = Obj.length; ...

  9. Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...

  10. CodeForces 81D.Polycarp's Picture Gallery 乱搞

    D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...