leetcode-mid-design-380. Insert Delete GetRandom O(1)
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)的更多相关文章
- 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)
[LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- LeetCode 380. Insert Delete GetRandom O(1)
380. Insert Delete GetRandom O(1) Add to List Description Submission Solutions Total Accepted: 21771 ...
- 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). ...
- [LeetCode] 380. Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- [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 ...
- LeetCode 380. Insert Delete GetRandom O(1) (插入删除和获得随机数 常数时间)
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- [leetcode]380. Insert Delete GetRandom O(1)常数时间插入删除取随机值
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- LeetCode 380. Insert Delete GetRandom O(1) 常数时间插入、删除和获取随机元素(C++/Java)
题目: Design a data structure that supports all following operations in averageO(1) time. insert(val): ...
- [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 ...
- 380. Insert Delete GetRandom O(1) 设计数据结构:在1的时间内插入、删除、产生随机数
[抄题]: Design a data structure that supports all following operations in average O(1) time. insert(va ...
随机推荐
- Linux FTP的安装与权限配置
ftp安装部分,操作步骤如下: 1.切换到root用户 2.查看是否安装vsftp,我这个是已经安装的. [root@localhost vsftpd]# rpm -qa |grep vsftpd v ...
- source与./执行的区别
source与./执行的区别作用:使文件生效区别:1.source不需要文件有可执行(x)权限,而./需要文件有x权限,否则报错2.source执行是在当前shell中执行,./在当前shell的子s ...
- Action获取请求参数的3中方式
方式一:Action本身作为Model对象,通过属性注入(Setter)方法讲参数数据封装到Action中 具体为:在Action中,提供和参数名相同的几个属性,并为其提供set方法,那么,该参数会被 ...
- zabbix-agent安装遇到的坑
问题: libc.so.6(GLIBC_2.14)(64bit) is needed by zabbix-agent-3.4.11-1.el7.x86_64 原因: 系统环境是 centos6 ,下载 ...
- puppet自动化安装服务
puppet自动化部署 主机环境: server(master)端:172.25.7.1(server1.example.com) client(agent)端:172.25.7.2 172.25.7 ...
- Big Data(七)MapReduce计算框架
二.计算向数据移动如何实现? Hadoop1.x(已经淘汰): hdfs暴露数据的位置 1)资源管理 2)任务调度 角色:JobTracker&TaskTracker JobTracker: ...
- Linux安装redis,启动配置不生效(指定启动加载配置文件)
一.今天有个同学问我,为什么明明安装了redis,修改了配置,启动的时候,配置还是不生效.如下图是安装后的redis文件图. 二.想加载上图中的redis.conf,进入到src中寻找到启动文件red ...
- python之路day14--列表生成式、生成器generator、生成器并行
列表生成式 列表生成式阅读量: 44 现在有个需求,现有列表a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],要求你把列表里的每个值加1,你怎么实现?你可能会想到2种方式 二逼青年 ...
- springboot maven打包插件
<build> <plugins> <!-- springboot maven打包--> <plugin> <groupId>org.spr ...
- 对html2canvas的研究
介绍 该脚本允许您直接在用户浏览器上截取网页或部分网页的“屏幕截图”.屏幕截图基于DOM,因此它可能不是真实表示的100%准确,因为它没有制作实际的屏幕截图,而是根据页面上可用的信息构建屏幕截图. 这 ...