题目:

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

1.insert(val): Inserts an item val to the set if not already present.
2.remove(val): Removes an item val from the set if present.
3.getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.
Example:

// Init an empty set.
RandomizedSet randomSet = new RandomizedSet();

// Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomSet.insert(1);

// Returns false as 2 does not exist in the set.
randomSet.remove(2);

// Inserts 2 to the set, returns true. Set now contains [1,2].
randomSet.insert(2);

// getRandom should return either 1 or 2 randomly.
randomSet.getRandom();

// Removes 1 from the set, returns true. Set now contains [2].
randomSet.remove(1);

// 2 was already in the set, so return false.
randomSet.insert(2);

// Since 2 is the only number in the set, getRandom always return 2.
randomSet.getRandom();
思路:肯定是借助java已有的数据结构进行设计,常用的有ArrayList,HashMap,HashSet,能做到随机取数的有前两个,能实现判断和删除O(1)是否包含的是后两个,
而且map想判断的话,数据必须存key,这样就不能取数了。用list取数的时候,必须要知道数据和下标的对应关系,所以可以map和list配合
 1 * 没有一个现成的数据结构可以符合要求,但是可以组合使用它们实现,list存数据,map的key存数据,value存数据在list中的下标,
2 * 这样ArrayList可以实现存O(1),删除借助map获取下标,实现O(1),判断重复用map的containsKey方法
3 * 其实insert和remove,map都可以单独实现O(1),不单独用map就是因为无法实现getRandom,因为由于判断重复需要containsKey方法,所以数据必须存key而不是value
4 * 但是随机取数时,map无法取出指定key,所以不能单独用map,随机取数只能用list,随机指定下标而取出数值
5 * getRandom要求知道数据结构的大小,并且存储元素是相邻的
6 public static void main(String[] args) {
7 Q380InsertDeleteGetRandomO1 q = new Q380InsertDeleteGetRandomO1();
8 q.set.insert(0);
9 q.set.insert(1);
10 q.set.remove(0);
11 System.out.println(q.set.list);
12 System.out.println(q.set.map);
13 System.out.println(q.set.getRandom());
14 }
15 class RandomizedSet {
16 HashMap<Integer,Integer> map;
17 ArrayList<Integer> list;
18 /** Initialize your data structure here. */
19 public RandomizedSet() {
20 map = new HashMap();
21 list = new ArrayList<>();
22 }
23
24 /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
25 public boolean insert(int val) {
26 //判断是否包含可以用map或者set,但是set不能存下标
27 if (!map.containsKey(val))
28 {
29 //key存数据,value存它在list中的下标
30 map.put(val,list.size());
31 list.add(val);
32 return true;
33 }
34 else
35 return false;
36 }
37
38 /** Removes a value from the set. Returns true if the set contained the specified element. */
39 public boolean remove(int val) {
40 if (map.containsKey(val))
41 {
42 //删除:要想map中的value不用全部变,只能删除的时候list的下标不全部改变(list默认删除后后边的元素会向前挪),多以采取的方法:
43 //先把list最后一个元素num覆盖到指定位置(借助map获取位置),再删除最后一个元素,然后map修改num的value,再删除val那条数据
44 //最后一个元素
45 int num = list.get(list.size()-1);
46 //覆盖(也可以两个元素交换)
47 list.set(map.get(val),num);
48 //删除最后一个元素
49 list.remove(list.size()-1);
50 //改变num的下标
51 map.put(num,map.get(val));
52 //删除val这条数据
53 map.remove(val);
54 return true;
55 }
56 else
57 return false;
58 }
59
60 /** Get a random element from the set. */
61 public int getRandom() {
62 Random random = new Random();
63 //随机获取下标进行取数
64 return list.get(random.nextInt(list.size()));
65 }
66 }
67
68 /**
69 * Your RandomizedSet object will be instantiated and called as such:
70 * RandomizedSet obj = new RandomizedSet();
71 * boolean param_1 = obj.insert(val);
72 * boolean param_2 = obj.remove(val);
73 * int param_3 = obj.getRandom();
74 */

map和set可不可以组合呢,想了想好像也可以,set只负责判断包含,map的存,删,随机取,都可以O(1),有待验证

[leetcode]380. Insert Delete GetRandom O(1)设计数据结构,实现存,删,随机取的时间复杂度为O(1)的更多相关文章

  1. 380. Insert Delete GetRandom O(1) 设计数据结构:在1的时间内插入、删除、产生随机数

    [抄题]: Design a data structure that supports all following operations in average O(1) time. insert(va ...

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

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

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

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

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

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

  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)常数时间插入删除取随机值

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

  8. LeetCode 380. Insert Delete GetRandom O(1) 常数时间插入、删除和获取随机元素(C++/Java)

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

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

随机推荐

  1. java导出excel并且压缩成zip上传到oss,并下载,使用字节流去存储,不用文件流保存文件到本地

    最近项目上要求实现导出excel并根据条数做分割,然后将分割后的多个excel打包成压缩包上传到oss服务器上,然后提供下载方法,具体代码如下:这里只展示部分代码,获取数据的代码就不展示了 ByteA ...

  2. PADS CAM光绘输出文件设置

    PADS CAM光绘输出文件设置 在使用PADS完成电路板的设计后,通常还需要在CAM350中经过一些处理生成Gerber文件,交给制板厂商进行电路板的生产,这就要求在PADS中设置生成CAM文件,然 ...

  3. Beta冲刺随笔——Day_Seven

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...

  4. 秒极啊!手把手带你进行shiro授权拦截器的重写,学到了学到了

    shiro整合前后端分离的springboots,Vue项目真的是有很多大坑啊. 今天我的主题是:如何设置shiro过滤器. 遇到问题:我的项目是前后端分离的,shiro里面有一个shiroFilte ...

  5. PyQt(Python+Qt)学习随笔:QTreeView树形视图的headerHiden属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的headerHiden属性用于控制视图中是否隐藏标题,为True隐藏,否 ...

  6. PyQt(Python+Qt)学习随笔:Qt Designer中部件的geometry几何属性

    geometry属性保存部件相对于其父级对象的位置和大小,Qt实际上是以一个长方形来表示部件的位置和大小的,包括左上角的坐标位置.长度和宽带. 当部件的geometry调整时,部件如果可见将立即接收m ...

  7. Hive 安装 & Mysql 安装

    安装Hive && mysql (1)安装Hive Hive安装所需要的依赖(安装Hive前必须先安装jdk.hadoop) 1)jdk1.6以上 2)Hadoop要启动 未安装jdk ...

  8. Windows下MySQL无法启动万能解决方案

    MySQL启动报错 window10上安装了MySQL之前使用都是执行net start mysql 启动,执行net stop mysql关闭. 某天开始启动报错"MySQL 服务无法启动 ...

  9. Outlook会议中人员窗格消失了

    Outlook会议中人员窗格消失了,如何恢复: 在[文件]-[信息]-[管理COM加载项] 启用Outlook social conector, 对启用,一直启用,再重启OUTLOOK即可.

  10. STL——容器(List)list 的大小操作

    ist.size(); //返回容器中元素的个数 1 #include <iostream> 2 #include <list> 3 4 using namespace std ...