设计一个数据结构满足O(1)的insert, delete和getRandom。这个是从地里Amazon的面经中看到的。

我们可以使用一个resizable数组arr以及一个HashMap来完成。

insert(value): 将value加入到数组中,然后将<value, index> pair放到hashmap里

delete(value):  交换value和数组末端元素交换,从hashmap里去除掉value,然后减少当前数组的长度count。

getRandom():先用Math.random()得到一个0 - count的随机数r,然后直接返回数组里的值arr[r]

这里的O(1)应该指的是average time。怎么处理重复的key是个小问题,可能要用一个set或者LinkedList来存储相同value的不同index,跟HashMap遇到collision的解决方法一样。

Reference:

http://www.geeksforgeeks.org/design-a-data-structure-that-supports-insert-delete-search-and-getrandom-in-constant-time/

http://puzzlersworld.com/interview-questions/ds-with-insert-delete-and-getrandomelement-in-o1/

http://stackoverflow.com/questions/5682218/data-structure-insert-remove-contains-get-random-element-all-at-o1

设计数据结构O1 insert delete和getRandom的更多相关文章

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

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

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

  3. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  4. [Swift]LeetCode380. 常数时间插入、删除和获取随机元素 | Insert Delete GetRandom O(1)

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

  5. [LeetCode] 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] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

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

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

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

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

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

随机推荐

  1. apache与tomcat负载集群的3种方法

    花了两天时间学习apache与tomcat的集成方法,现在把学习成果记录下来. apache与tomcat负载集群集成方法有3种jk.jk_proxy.http_proxy 本次集成使用的软件版本: ...

  2. JQuery常用实用的事件[较容易忽略的方法]

     JQuery常用实用的事件 注:由于JQuery片段较多就没有用插入代码文本插入,请见谅!JQuery 事件处理ready(fn)代码: $(document).ready(function(){ ...

  3. ffmpeg 命令

    1.保存文件: ffmpeg -i rtsp://admin:12345@172.29.61.108/Streaming/Channels/1 -vcodec copy -acodec libvo_a ...

  4. 图解Git/图形化的Git参考手册

    此页图解git中的最常用命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透彻. 基本用法 上面的四条命令在工作目录.暂存目录(也叫做索引)和仓库之间复制文件. ● git add fil ...

  5. ptype_base和ptype_all学习笔记

    "linux-2.6.32/include/linux/netdevice.h" struct packet_type { __be16 type; /* This is real ...

  6. DOM中事件绑定补充方法

    先将上一篇文章中提到的为元素增加事件的方法和移除事件的方法拿过来: <span style="font-size:18px;">//跨浏览器添加事件 function ...

  7. W3C和IE中的事件处理函数

    在上一篇文章中提到了关于传统的JS中注册事件对象的一些缺点和问题,下面是关于DOM2级的现代事件绑定.本文中设计到的HTML文件在文章最后 一.W3C事件处理函数 “DOM2 级事件”定义了两个方法, ...

  8. [工作积累] Software keyboard not shown on Activity.onCreate

    protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setCo ...

  9. iOS Automation Test

    google resource for KIF: http://www.oschina.net/translate/ios-ui-testing-with-kif

  10. HDOJ 1069 DP

    开启DP之路 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 描述一下: 就是给定N(N<=20)个方体,让你放置,求放置的最高高度,限制条件 ...