380. Insert Delete GetRandom O(1)
来源:互联网 发布:新手怎样开淘宝店 编辑:程序博客网 时间:2024/06/06 02:51
Design a data structure that supports all following operations in average O(1) time.
insert(val)
: Inserts an item val to the set if not already present.remove(val)
: Removes an item val from the set if present.getRandom
: Returns a random element from current set of elements. Each element must have the same probability of being returned.// Init an empty set.RandomizedSet randomSet = new RandomizedSet();// Inserts 1 to the set. Returns true as 1 was inserted successfully.randomSet.insert(1);// Returns false as 2 does not exist in the set.randomSet.remove(2);// Inserts 2 to the set, returns true. Set now contains [1,2].randomSet.insert(2);// getRandom should return either 1 or 2 randomly.randomSet.getRandom();// Removes 1 from the set, returns true. Set now contains [2].randomSet.remove(1);// 2 was already in the set, so return false.randomSet.insert(2);// Since 2 is the only number in the set, getRandom always return 2.randomSet.getRandom();
这道题刚开始做的时候大意了,没有太明白题目意思,常用数据结构中基本都能完成常量时间内添加元素功能,但是在常量时间内删除元素功能例如数组就不能完成,因此需要诸如Map、Set这类可以在常量时间内定位到元素的数据结构,另外一个要求是需要返回随机元素,这时就需要我们的元素有一定顺序,因此需要类似于数组之类的数据结构,最后选用HashMap和ArrayList完成该数据结构的创建,代码如下:
ArrayList<Integer> arrayList = null; HashMap<Integer,Integer> hashMap = null; /** Initialize your data structure here. */ public RandomizedSet() { arrayList = new ArrayList<Integer>(); hashMap = new HashMap<Integer, Integer>(); } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ public boolean insert(int val) { boolean result = hashMap.containsKey(val); if(result){ return false; }else{ hashMap.put(val, arrayList.size()); arrayList.add(val); return true; } } /** Removes a value from the set. Returns true if the set contained the specified element. */ public boolean remove(int val) { boolean result = hashMap.containsKey(val); int pos = 0; if(result){ pos = hashMap.get(val); arrayList.set(pos, arrayList.get(arrayList.size() - 1)); hashMap.put(arrayList.get(pos), pos); }else{ return false; } hashMap.remove(val); arrayList.remove(arrayList.size() - 1); return true; } /** Get a random element from the set. */ public int getRandom() { // Random rand = new Random(); // return arrayList.get(rand.nextInt(arrayList.size())); int num = (int)(Math.random() * arrayList.size()); return arrayList.get(num); }
0 0
- [leetcode] 380. Insert Delete GetRandom O(1)
- leetcode 380.Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- [LeetCode]380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 【Leetcode】380. Insert Delete GetRandom O(1)
- LeetCode 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 380. Insert Delete GetRandom O(1)
- 【leetcode】380. Insert Delete GetRandom O(1)
- 使用PMD进行代码审查
- Kilim框架的Actor并发模型
- Xcode 8.3 (iOS 10.3)新增应用内评价API 之 SKStoreReviewController
- 学习感觉
- 【记录】2种随机迷宫生成算法的cpp实现
- 380. Insert Delete GetRandom O(1)
- Ogre嵌入Qt:布告板显示中文(补充)
- 资料_编程_大杂烩
- Java并发编程总结
- poj1703 Find them, Catch them
- 在eclipse上搭建mapreduce开发环境及运行wordcount
- Hue安装配置实践
- hdu 1027 Ignatius and the Princess II (全排列)
- 噩梦射手(SurvivalShooter)教程(四)