https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/

public class RandomizedCollection {
ArrayList<Integer> nums;
HashMap<Integer, Set<Integer>> locs;
java.util.Random rand; /** Initialize your data structure here. */
public RandomizedCollection() {
rand = new java.util.Random();
nums = new ArrayList<Integer>();
locs = new HashMap<Integer, Set<Integer>>();
} /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
boolean contained = locs.containsKey(val);
if (!contained) {
locs.put(val, new LinkedHashSet<Integer>());
}
locs.get(val).add(nums.size());
nums.add(val);
return !contained;
} /** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
boolean contained = locs.containsKey(val);
if (!contained) {
return false;
}
int index = locs.get(val).iterator().next();
locs.get(val).remove(index);
if (index < nums.size()-1) {
int change = nums.get(nums.size()-1);
nums.set(index, change);
locs.get(change).remove(nums.size()-1);
locs.get(change).add(index);
}
nums.remove(nums.size()-1);
if (locs.get(val).isEmpty()) {
locs.remove(val);
}
return true; } /** Get a random element from the collection. */
public int getRandom() {
return nums.get(rand.nextInt(nums.size()));
}
} /**
* Your RandomizedCollection object will be instantiated and called as such:
* RandomizedCollection obj = new RandomizedCollection();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/

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

  1. 381. 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 380. Insert Delete GetRandom O(1) 、381. Insert Delete GetRandom O(1) - Duplicates allowed

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

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

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

  4. [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 ...

  5. [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 ...

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

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

  7. LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复(C++/Java)

    题目: Design a data structure that supports all following operations in averageO(1) time. Note: Duplic ...

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

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

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

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

  10. [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 ...

随机推荐

  1. Cygwin镜像使用

    前言:Cygwin是一个在windows平台上运行的类UNIX模拟环境,可以自己安装想用的插件 Cygwin镜像使用帮助 收录架构 x86 x86_64 收录版本 所有版本 更新时间 每12小时更新一 ...

  2. ThinkPHP导入第三方类库Vendor

    详情查看ThinkPHP3.2手册 架构 > 自动加载 章节 vendor('Uploader','','.class.php')

  3. win10 远程桌面远程电脑时每次要输入密码及身份验证错误,要求的函数不受支持问题解决

    解决以前每次远程时能能记住密码,更新系统补丁后现在每次登录要输入密码了及远程时提示身份验证错误,要求的函数不受支持问题 解决方法一.卸载更新安装的新补丁,远程桌面正常,能记住密码 解决方法二.修改注册 ...

  4. UVA - 10815 - Andy's First Dictionary STL

    Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him ...

  5. 为什么Java7开始在数字中使用下划线

    JDK1.7的发布已经介绍了一些有用的特征,尽管大部分都是一些语法糖,但仍然极大地提高了代码的可读性和质量.其中的一个特征是介绍字面常量数字的下划线.从Java7开始,你就可以在你的Java代码里把长 ...

  6. webstorm2018最新激活码license server

    2018.7.5最新激活码: license server:https://s.tuzhihao.com:666/ 以后持续更新....

  7. DHTML和HTML有什么区别?有什么不同

    DHTML和HTML有什么区别?有什么不同 首先Dynamic HTML是一种制作网页的方式,而不是一种网络技术(就像JavaScript和ActiveX):它也不是一个标记.一个插件或者是一个浏览器 ...

  8. mysql数据库外键删除更新规则

    1.CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行. 2.SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL.如果使用该选项,必须保证子表列没有指定NOT NULL. ...

  9. BZOJ.2460.[BeiJing2011]元素(线性基 贪心)

    题目链接 线性基:https://blog.csdn.net/qq_36056315/article/details/79819714. \(Description\) 求一组矿石,满足其下标异或和不 ...

  10. 周末 “CTO训练营”

    今天下午去中关村参加了51cto高招 “CTO训练营”  第一期. 呃蛮有收获,聊技术发展,技术cto线路或对应发展,人事对应cto发展,投资人对应看法,51cto老总的看法. 呃,挺有意思,同样认识 ...