381. O(1) 时间插入、删除和获取随机元素 - 允许重复

设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构。

注意: 允许出现重复元素。

insert(val):向集合中插入元素 val。

remove(val):当 val 存在时,从集合中移除一个 val。

getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。

示例:

// 初始化一个空的集合。
RandomizedCollection collection = new RandomizedCollection(); // 向集合中插入 1 。返回 true 表示集合不包含 1 。
collection.insert(1); // 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。
collection.insert(1); // 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。
collection.insert(2); // getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
collection.getRandom(); // 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。
collection.remove(1); // getRandom 应有相同概率返回 1 和 2 。
collection.getRandom();
//维护一个list存储所有val的值
//维护一个map存储每个val在List的位置
//随机只需要再list随机
//删除操作是将所要删除的数将在list中最后一个位置的数放到所要删除的位置
//在更新一下最有一个数在map中的位置即可
class RandomizedCollection { private Map<Integer,Set<Integer>> map ; private List<Integer> list ; private Random random ; private int size = 0 ; public RandomizedCollection() {
map = new HashMap<>() ;
list = new ArrayList<>() ;
random = new Random() ;
} public boolean insert(int val) {
if(map.containsKey(val)){
Set<Integer> indexes = map.get(val) ;
list.add(size,val) ;
indexes.add(size) ;
size++ ;
return false ;
}else{
Set<Integer> indexes = new HashSet<>() ;
map.put(val,indexes) ;
list.add(size,val) ;
indexes.add(size) ;
size++ ;
return true ;
}
} public boolean remove(int val) {
if(!map.containsKey(val)){
return false ;
}
Set<Integer> indexes = map.get(val) ;
if(list.get(size-1) == val){
indexes.remove(size-1) ;
size-- ;
}else{
//删除下标
Iterator<Integer> it = indexes.iterator() ;
int index = it.next() ;
it.remove();
//把list的最后一个值放到对应下标那
int last = list.get(size-1) ;
list.set(index,last) ;
Set<Integer> set = map.get(last) ;
//更改我最后一个值的下标
set.remove(size-1) ;
set.add(index) ;
size-- ;
}
if(indexes.size() == 0){
map.remove(val) ;
}
return true ;
} public int getRandom() {
return list.get(random.nextInt(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();
*/

最新文章

  1. C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。
  2. 在SqlServer2008R2中,在一张表上加上insert、update、delete触发器(带游标)
  3. mysql启动失败:不能创建pid文件
  4. 鼠标光标聚焦到可编辑div的最末尾
  5. 分组背包——sicily 1750
  6. C#-WinForm-如何获取文本框(TextBox)中鼠标,光标位置
  7. jquery笔记(遍历)
  8. Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系
  9. Zend-MVC intro
  10. C/C++运算符优先级
  11. [JavaEE,MVC] Struts工作原理
  12. ibatis.net 多线程的调试
  13. spoj 39
  14. 【CF】7 Beta Round D. Palindrome Degree
  15. [TypeScript] Loading Compiled TypeScript Files in Browser with SystemJS
  16. 计算机视觉的matlab工具箱及MVG等
  17. DOM模型结构——节点类型
  18. ACdream 之ACfun 题解
  19. salt自动化部署
  20. python专题-Mysql数据库(python2._+ Mysqldb)

热门文章

  1. nodejs开发准备工作(1)
  2. android实现计时器
  3. 【FreeRTOS学习02】源码结构/数据类型/命名规则总结
  4. scala 隐式参数
  5. spring data jpa 多对多 ManyToMany
  6. bash初始化小建议
  7. php基本语法学习
  8. Hadoop CDH版本安装和启动(CentOS7)
  9. 【Jmeter学习】【第一节】【Jmeter的安装】
  10. 【Linux学习】【第一节】【vi命令】