mycode

import random
class RandomizedSet(object): def __init__(self):
"""
Initialize your data structure here.
"""
self.s = [] def insert(self, val):
"""
Inserts a value to the set. Returns true if the set did not already contain the specified element.
:type val: int
:rtype: bool
"""
if val in self.s: return False
else:
self.s.append(val)
return True def remove(self, val):
"""
Removes a value from the set. Returns true if the set contained the specified element.
:type val: int
:rtype: bool
"""
if val in self.s:
self.s.remove(val)
return True
else:
return False def getRandom(self):
"""
Get a random element from the set.
:rtype: int
"""
return self.s[random.randint(0,len(self.s)-1)] # Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()

参考:

emmmm。。。我直接调用的???我的天。。。

思路:用dic来查找,找到后去insert和删除

import random

class RandomizedSet(object):

    def __init__(self):
self.data = []
self.pos = {} def insert(self, val):
if val in self.pos:
return False self.data.append(val)
self.pos[val] = len(self.data) - 1 return True def remove(self, val):
"""
Removes a value from the set. Returns true if the set contained the specified element.
:type val: int
:rtype: bool
"""
if val not in self.pos:
return False last = self.data[-1]
elt = self.pos[val]
self.data[elt] = last
self.pos[last] = elt
self.data.pop()
del self.pos[val] return True def getRandom(self):
return random.choice(self.data) # Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()

leetcode-mid-design-380. Insert Delete GetRandom O(1)的更多相关文章

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

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

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

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

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

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

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

  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]380. Insert Delete GetRandom O(1)设计数据结构,实现存,删,随机取的时间复杂度为O(1)

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

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

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

随机推荐

  1. synchronize和lock的区别 & synchionzie与volatile的区别

    synchronized与Lock的区别 https://www.cnblogs.com/iyyy/p/7993788.html Lock和synchronized和volatile的区别和使用 ht ...

  2. .net几种timer区别

    概述:.net框架不同名称控件都包含了各种timer,但每个timer有什么具体区别呢? 一.System.Threading private static void ThreadingTimer() ...

  3. Linux之RedHat7如何更换yum源

    目前,我们常见的系统大概就是Windows.Linux和Mac OS了.Windows系统应该是大部分人最早开始接触的系统,毕竟Windows系统使用起来相当方便,只需要点点鼠标,外加会简单的打字,一 ...

  4. 创建AIX克隆盘

    1.AIX的克隆盘技术 AIX克隆盘,AIX rootvg的备用替换盘,可以用于保留AIX的原始状态,使AIX在进行升级操作时保留一个AIX操作系统的原始映像,在系统需要时实现即时还原,回到升级操作前 ...

  5. mariadb读写分离

    mycat maraidb主从架构,是主负责写,从负责读,但前端如果没有调度器的话,是无法实现读写分离的.这就涉及到了中间站,它就是mycat.一定要在主从架构的基础之上实现读写分离. 配置三台的主从 ...

  6. 修改docker自定义网桥后的默认网卡名称

    [root@docker2 ~]# docker  network   create  --subnet=10.10.10.0/24  docker1   #新键网桥docker142323044a4 ...

  7. SQL函数 Convert,dateadd

    1.SQL中CONVERT转化函数的用法 格式:CONVERT(data_type,expression[,style])说明:此样式一般在时间类型(datetime,smalldatetime)与字 ...

  8. python 文件夹压缩

    import os import zipfile def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpath: ...

  9. pt-archiver使用记录

    pt-archiver使用记录 功能:将MySQL表中的行存档到另一个表或文件中用法:pt-archiver [OPTIONS] --source DSN --where WHERE ; trunca ...

  10. Leaflet使用vector tiles样式设置

    //point style var myIcon = L.icon({ iconUrl: 'css/images/dian.svg', // shadowUrl: 'css/images/leaf-s ...