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 ...
随机推荐
- C#下载图片,用户选择保存路径
Html代码 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></ti ...
- Jade学习(四)之结合node如何编译执行
1.首先安装node 2.新建一个文件夹并进入该文件夹 3.安装jade 4.在新建的文件夹下新建js文件,写nodejs代码 5.在vscode中利用插件code runner直接执行js文件,输出 ...
- Jquery复习(三)之链式调用
通过 jQuery,可以把动作/方法链接在一起. Chaining 允许我们在一条语句中运行多个 jQuery 方法(在相同的元素上). jQuery 方法链接 直到现在,我们都是一次写一条 jQue ...
- 使用Python的pandas-datareader包下载雅虎财经股价数据
0 准备工作 首先,使用pip方法安装pandas和pandas-datareader两个功能包. 安装的方法十分简单,以管理员身份运行cmd. 输入以下命令. $ pip install panda ...
- a标签前端下载火狐兼容和笔记
1.a标签实现前端下载的谷歌兼容 我们都知道,文件下载的一种实现方案就是后端返回文件流,然后前端进行生成a标签并触发点击来下载.但是在火狐浏览器的时候,需要注意一些兼容性问题.原因是火狐的同源策略.官 ...
- Find the hotel HDU - 3193 (ST表RMQ)
Summer again! Flynn is ready for another tour around. Since the tour would take three or more days, ...
- Quartz(一)
1 Quartz介绍 定时任务,无论是互联网公司还是传统的软件行业都是必不可少的,Quartz是好多优秀的定时任务开源框架的基础的. 我们应用最简单和最基础的配置,不需要太多参数,就可以轻松掌握企业中 ...
- webpack命令:Module build failed(from ./node_modules/babel-loader/lib/index.js)/405/错误解决
在项目中运行的时候出现报错,错误为Module build failed (from ./node_modules/babel-loader/lib/index.js) 解决方案: 控制台输入 np ...
- 【Linux】CentOS6上mysql5.7安装
1.下载安装yum源 根据系统下载yum源 https://dev.mysql.com/downloads/repo/yum/ rpm -ivh xxxxx.rpm 2.修改yum源 vim /etc ...
- 在centos7.5使用DockerFile构建镜像时报错“Error parsing reference: "microsoft/dotnet:2.2-aspnetcore-runtime AS base" is not a valid repository/tag: invalid reference format”
运行dockerfile时报出的错误 FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base Error parsing reference: &qu ...