// 参考了下面一些讨论的解法
// 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. [CodeForces]CodeForces 13D 几何 思维

    大致题意: 给出N个红点和M个蓝点,问可以有多少个红点构成的三角形,其内部不含有蓝点 假设我们现在枚举了一条线段(p[i],p[j]),我们可以记录线段下方满足(min(p[i].x,p[j].x)& ...

  2. The file will have its original line endings in your working directory.

    在空仓库的情况下,add,出现一下问题 The file will have its original line endings in your working directory. 当报这个警告时是 ...

  3. H5 video标签视频加载存在的问题

    客户发现上传的视频无法播放,然后主管让我解决这个问题,这个页面不是我负责的,我看了代码,发现视频用的h5标签video标签加载视频.我看了没问题,然后 我先用ie浏览器打开,视频加载没问题.然后我给主 ...

  4. 移动端meta标签

    现在的手机或平板电脑等移动设备上的浏览器默认都有双击放大的设置,如何阻止双击放大?user-scalable=no <!-- 禁止缩放 --> <meta name=”viewpor ...

  5. leetcode 无重复字符的最长子串 python实现

    这道题需要借助哈希查找key的O(n) 时间复杂度, 否则就会超时 初始化一个 哈希表\字典  dic 头指针start 初始为0 当前指针 cur 初始为0 最大长度变量 l 初始为0 用cur变量 ...

  6. 维护直线的线段树:Bzoj1568,Bzoj3938(Uoj88)

    有这样一类线段树,可以维护一些直线方程并对每个点求出最大值. 首先先看BZOJ1568,输入给你条直线的方程,你需要对于指定的位置求出最大的函数值. 看到数据范围nlog^2n可做,考虑用线段树去维护 ...

  7. [Assembly]汇编编写简易键盘记录器

    环境:Windows xp sp3工具:masmnotepad++ 首先列出本次编程程序要执行的步骤:(1).读取键盘所输入的字符(2).输出到屏幕上(3).完善Esc.Backspace.空格.回车 ...

  8. hdu 5248 贪心

    题意:

  9. bzoj 1336 最小圆覆盖

    最小圆覆盖 问题:给定平面上的一个点集,求半径最小的一个圆,使得点集中的点都在其内部或上面. 随机增量算法: 定义:点集A的最小圆覆盖是Circle(A) 定理:如果Circle(A)=C1,且a不被 ...

  10. Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题

    A. Little Pony and Expected Maximum Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...