leetcode380
class RandomizedSet {
public:
/** Initialize your data structure here. */
RandomizedSet() {
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val) {
if(hash.count(val)) return false;
hash[val] = vec.size();
vec.push_back(val);
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
if(hash.count(val)==) return false;
int pos = hash[val];
hash[vec.back()] = pos;
swap(vec[pos], vec[vec.size()-]);
hash.erase(val);
vec.pop_back();
return true;
}
/** Get a random element from the set. */
int getRandom() {
return vec[rand()%vec.size()];
}
private:
unordered_map<int, int> hash;
vector<int> vec;
};
/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* bool param_1 = obj.insert(val);
* bool param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/
数据结构设计问题,从网上找的参考答案。
根据以上的思路,写了一份python版本的代码:
class RandomizedSet:
def __init__(self):
"""
Initialize your data structure here.
"""
self.dic = dict()
self.l = list()
self.length = def insert(self, val: int) -> bool:
"""
Inserts a value to the set. Returns true if the set did not already contain the specified element.
"""
if val in self.dic:
return False
else:
self.dic[val] = self.length
self.l.append(val)
self.length +=
return True def remove(self, val: int) -> bool:
"""
Removes a value from the set. Returns true if the set contained the specified element.
"""
if val not in self.dic:
return False
else:
self.dic.pop(val)
self.l.remove(val)
self.length -=
return True def getRandom(self) -> int:
"""
Get a random element from the set.
"""
rnd = int(random.uniform(, self.length))
key = self.l[rnd]
return key
leetcode380的更多相关文章
- LeetCode380 常数时间插入、删除和获取随机元素
LeetCode380 常数时间插入.删除和获取随机元素 题目要求 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构. insert(val):当元素 val 不存在时,向集合中插 ...
- [Swift]LeetCode380. 常数时间插入、删除和获取随机元素 | Insert Delete GetRandom O(1)
Design a data structure that supports all following operations in averageO(1) time. insert(val): Ins ...
- 2017-3-8 leetcode 380 381 532
l两周以来,第一次睡了个爽,开心! ================================= leetcode380 https://leetcode.com/problems/insert ...
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复(C++/Java)
题目: Design a data structure that supports all following operations in averageO(1) time. Note: Duplic ...
随机推荐
- CF 148D D. Bag of mice (概率DP||数学期望)
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...
- 监听器(Listener)学习(二)
一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...
- BZOJ4373 算术天才⑨与等差数列 【线段树】*
BZOJ4373 算术天才⑨与等差数列 Description 算术天才⑨非常喜欢和等差数列玩耍. 有一天,他给了你一个长度为n的序列,其中第i个数为a[i]. 他想考考你,每次他会给出询问l,r,k ...
- 当我们使用 MVVM 模式时,我们究竟在每一层里做些什么?
这篇文章不会说 MVVM 是什么,因为讲这个的文章太多了:也不会说 MVVM 的好处,因为这样的文章也是一搜一大把.我只是想说说我们究竟应该如何理解 M-V-VM,当我们真正开始写代码时,应该在里面的 ...
- js中怎么去掉数组的空值
for(var i = 0 ;i<array.length;i++) { if(array[i] == "" || typeof(array[i] ...
- 《DSP using MATLAB 》示例Example6.3
代码: C0 = 0; B1 = [2 4; 3 1]; A1 = [1 1 0.9; 1 0.4 -0.4]; B2 = [0.5 0.7; 1.5 2.5; 0.8 1]; A2 = [1 -1 ...
- 通过 Windows API 获取鼠标位置等状态信息
#include <graphics.h> #include <stdio.h> void main() { initgraph(, ); // 初始化绘图窗口 HWND hw ...
- L2TP/IPSec一键安装脚本
本脚本适用环境:系统支持:CentOS6+,Debian7+,Ubuntu12+内存要求:≥128M更新日期:2017 年 05 月 28 日 关于本脚本:名词解释如下L2TP(Layer 2 Tun ...
- 配置Jar包及相关依赖Jar包的本地存放路径
配置Jar包及相关依赖Jar包的本地存放路径 用 maven2 ,pom.xml中设置了依赖,会帮你下载所有依赖的.jar到 M2_REPO 指向的目录. M2_REPO是一个用来定义 maven 2 ...
- GOF23设计模式之策略模式(strategy)
一.策略模式概述 策略模式对应于解决某一个问题的一个算法族,允许用户从该算法族中任选一种算法解决一个问题,同时可以方便的更换算法或者增加新的算法.并且由客户端决定调用哪个算法. 策略模式的本质: 分离 ...