当前位置:网站首页>leetcode--380.O(1) 时间插入、删除和获取随机元素
leetcode--380.O(1) 时间插入、删除和获取随机元素
2022-04-23 13:41:00 【爱学习的Amelia】
- 题目:实现
RandomizedSet类:
RandomizedSet()初始化RandomizedSet对象bool insert(int val)当元素val不存在时,向集合中插入该项,并返回true;否则,返回false。bool remove(int val)当元素val存在时,从集合中移除该项,并返回true;否则,返回false。int getRandom()随机返回现有集合中的一项(测试用例保证调用此方法时集合中至少存在一个元素)。每个元素应该有 相同的概率 被返回。
你必须实现类的所有函数,并满足每个函数的 平均 时间复杂度为O(1)。1
- Example :
# Example 1
输入
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
输出
[null, true, false, true, 2, true, false, 2]
解释
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // 向集合中插入 1 。返回 true 表示 1 被成功地插入。
randomizedSet.remove(2); // 返回 false ,表示集合中不存在 2 。
randomizedSet.insert(2); // 向集合中插入 2 。返回 true 。集合现在包含 [1,2] 。
randomizedSet.getRandom(); // getRandom 应随机返回 1 或 2 。
randomizedSet.remove(1); // 从集合中移除 1 ,返回 true 。集合现在包含 [2] 。
randomizedSet.insert(2); // 2 已在集合中,所以返回 false 。
randomizedSet.getRandom(); // 由于 2 是集合中唯一的数字,getRandom 总是返回 2 。
- 提示:
-231 <= val <= 231 - 1- 最多调用
insert、remove和getRandom函数2 * 105次 - 在调用
getRandom方法时,数据结构中 至少存在一个 元素。
- 解法一–列表:
class RandomizedSet:
def __init__(self):
self.element = []
self.index = 0
def insert(self, val):
if val in self.element:
return False
else:
self.element.append(val)
return True
def remove(self, val):
if val in self.element:
self.element.remove(val)
return True
else:
return False
def getRandom(self):
return random.choice(self.element)
- 解法二–集合:
class RandomizedSet:
def __init__(self):
self.element = []
self.index = 0
def insert(self, val):
if val in self.element:
return False
else:
self.element.append(val)
return True
def remove(self, val):
if val in self.element:
self.element.remove(val)
return True
else:
return False
def getRandom(self):
return random.choice(self.element)
- 总结:有一个错误要注意的地方,没有搞清楚类中定义的元素,假如你想要这个元素随着类的变化而变化,就把他定义到初始化方法里面,即
__init__。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/insert-delete-getrandom-o1 ︎
版权声明
本文为[爱学习的Amelia]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46361294/article/details/124361983
边栏推荐
- GDB的使用
- Special window function rank, deny_ rank, row_ number
- Plato farm, a top-level metauniverse game, has made frequent positive moves recently
- 函数只执行第一次的执行一次 once函数
- 切线空间(tangent space)
- Common types and basic usage of input plug-in of logstash data processing service
- [code analysis (3)] communication efficient learning of deep networks from decentralized data
- Personal learning related
- Usereducer basic usage
- At the same time, the problems of height collapse and outer margin overlap are solved
猜你喜欢

Detailed explanation of constraints of Oracle table

SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置

Handling of high usage of Oracle undo

Common types and basic usage of input plug-in of logstash data processing service

Dolphin scheduler source package Src tar. GZ decompression problem

Apache seatunnel 2.1.0 deployment and stepping on the pit

SQL learning window function

【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享

Ai21 labs | standing on the shoulders of giant frozen language models

TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
随机推荐
Oracle job scheduled task usage details
解决方案架构师的小锦囊 - 架构图的 5 种类型
Parameter comparison of several e-book readers
Set Jianyun x Feishu Shennuo to help the enterprise operation Department realize office automation
ACFs file system creation, expansion, reduction and other configuration steps
Modification of table fields by Oracle
PG SQL intercepts the string to the specified character position
Solution of discarding evaluate function in surprise Library
Oracle creates tablespaces and modifies user default tablespaces
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
Influence of openssh version on SSH mutual trust creation in RAC environment
Using Jupiter notebook in virtual environment
Common types and basic usage of input plug-in of logstash data processing service
Oracle database recovery data
【vmware】vmware tools 地址
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
Get the attribute value difference between two different objects with reflection and annotation
19c RAC steps for modifying VIP and scanip - same network segment
Android 面试主题集合整理
Oracle index status query and index reconstruction