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 ...
随机推荐
- sync or async connect redis in golang
Head of head 在golang的整个生态里,redis client lib全部都使用多连接或者连接池.这是让人难以理解的,所以我和xiaofei一起写了一个同时支持同步和异步的redis ...
- APP开发浅谈-Fiddler抓包详解
Fiddler抓包工具在APP开发过程中使用非常频繁,对开发者理解HTTP网络传输原理以及分析定位网络方面的问题非常有帮助.今天抽点时间出来总结一下Fiddler在实际开发过程中的应用. 我开发过程中 ...
- 《DSP using MATLAB》示例Example 8.4
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 字符编码:ASCII,Unicode和UTF-8
字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得一点字符编码的知识. 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两 ...
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递 【转】
原文:http://www.cnblogs.com/lori/p/4045633.html 下面定义一个复杂类型对象 public class User_Info { public int Id { ...
- Django之Models
1.数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库. <1> sqlite django默认使用sqlite的数据库,默 ...
- Ambari HDP集群搭建文档
一.配置主机和节点机器之间SSH无密登录 多台外网服务器配置时,需要在/etc/hosts中把本机的IP地址设置为内网IP地址 http://2d67df38.wiz02.com/share/s/0J ...
- 国外牛人的五个Kubernetes学习建议
现在云中存在的许多系统都是建立在基于虚拟机,虚拟磁盘等物理概念的抽象基础之上的,”Heptio的联合创始人兼首席技术官兼Kubernetes在Google的原始开发人员之一的Joe Beda说. “K ...
- MYSQL子查询例题以及答案
More Subqueries Quizzes Above is the ERD for the database again - it might come in handy as you tack ...
- awk&sed 小实例
1.打印文件奇数行sed -n 'p;n'sed 'n;d' sed -n '$!N;P'sed -n '1~2p'awk 'i=!i'awk 'NR%2'2.打印文件偶数行sed -n 'n;p's ...