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. kafka工作原理

    https://blog.csdn.net/qq_29186199/article/details/80827085 https://www.jianshu.com/p/4bf007885116 ht ...

  2. docker Dockerfile文件的编写部分命令

    镜像的构建过程:编写dockerfile文件,执行docker build 进行构建镜像,最后docker run 运行容器镜像实例. docker build -t mycentos:1.01 . ...

  3. Vue+ElementUI学习总结(转载)

    Vue框架简介 Vue是一套构建用户界面的框架, 开发只需要关注视图层, 它不仅易于上手,还便于与第三方库或既有项目的整合.是基于MVVM(Model-View-ViewModel)设计思想.提供MV ...

  4. vue打包时,assets目录 和static目录下文件的处理区别(nodeModule中插件源码修改后,打包后的文件应放在static目录)

    为了回答这个问题,我们首先需要了解Webpack如何处理静态资产.在 *.vue 组件中,所有模板和CSS都会被 vue-html-loader 及 css-loader 解析,并查找资源URL.例如 ...

  5. 苹果浏览器和ios中,时间字符串转换问题

    背景:在开发PC端项目和小程序时,遇到过一个时间字符串转化问题,在苹果浏览器和ios微信客户端里,"2018-10-15 18:20" 以 字符"-"拼接的时间 ...

  6. css3 加载动画

    代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  7. TCP Retransmission 连接超时

    TCP Retransmission 连接超时 kame 2019/3/17 33 TCP 记一次TCP 连接超时 背景 用户反馈 >> 有出现支付超时.页面问题 (部分情况会出现) 分析 ...

  8. Oracle 之 触发器

    触发器是特定的事件出现的时候,自动隐式执行代码块,这个过程用户无法控制,用户只能控制触发的事件,触发后的操作,触发过程是自动执行的. 定义触发器: 1.名称 2.触发时间:是在执行事件之前(befor ...

  9. Linux下单机部署ELK日志收集、分析环境

    一.ELK简介 ELK是elastic 公司旗下三款产品ElasticSearch .Logstash .Kibana的首字母组合,主要用于日志收集.分析与报表展示. ELK Stack包含:Elas ...

  10. IO体系总结

    字节输入流: 字节输出流: 字符输入流: 字符输出流: