Insert Delete GetRandom O(1) - Duplicates allowed
来源:互联网 发布:淘宝店铺租用多少钱 编辑:程序博客网 时间:2024/05/24 08:34
Design a data structure that supports all following operations in average O(1) time.
Note: Duplicate elements are allowed.insert(val)
: Inserts an item val to the collection.remove(val)
: Removes an item val from the collection if present.getRandom
: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.
Example:
// Init an empty collection.RandomizedCollection collection = new RandomizedCollection();// Inserts 1 to the collection. Returns true as the collection did not contain 1.collection.insert(1);// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].collection.insert(1);// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].collection.insert(2);// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.collection.getRandom();// Removes 1 from the collection, returns true. Collection now contains [1,2].collection.remove(1);// getRandom should return 1 and 2 both equally likely.collection.getRandom();
思路:能够接受重复就是 HashMap<Integer, HashSet<Integer>> 就行了。然后按照跟Insert Delete GetRandom O(1) 一样的思想进行更新,也就是把后面的拿到前面来,后面的 移除,注意,这里有2个地方需要注意的是:
1. hashset生成的时候,需要put进hashmap里面;
2. 删除的时候,最后的一个index也许就是当前的index,也就是说,list里面只剩一个了,这个时候需要判断index< list.size()-1,如果小,那么就更新,否则就跳过更新直接删除。
public class RandomizedCollection { /** Initialize your data structure here. */ List<Integer> list; HashMap<Integer, HashSet<Integer>> hashmap; public RandomizedCollection() { list = new ArrayList<Integer>(); hashmap = new HashMap<Integer, HashSet<Integer>>(); } /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ public boolean insert(int val) { HashSet<Integer> hashset = hashmap.get(val); if(hashset == null){ hashset = new HashSet<Integer>(); hashmap.put(val, hashset); } list.add(val); hashset.add(list.size()-1); return true; } /** Removes a value from the collection. Returns true if the collection contained the specified element. */ public boolean remove(int val) { if(!hashmap.containsKey(val)){ return false; } else { int lastval = list.get(list.size()-1); HashSet<Integer> oldhashset = hashmap.get(val); int index = oldhashset.iterator().next(); oldhashset.remove(index); if(index < list.size()-1){ list.set(index, lastval); hashmap.get(lastval).add(index); hashmap.get(lastval).remove(list.size()-1); } list.remove(list.size()-1); if(oldhashset.size() == 0){ hashmap.remove(val); } return true; } } /** Get a random element from the collection. */ public int getRandom() { Random random = new Random(); int index = random.nextInt(list.size()); return list.get(index); }}
0 0
- Insert Delete GetRandom O(1) - Duplicates allowed
- Insert Delete GetRandom O(1) - Duplicates allowed
- Insert Delete GetRandom O(1) - Duplicates allowed
- LeetCode—381. Insert Delete GetRandom O(1) - Duplicates allowed
- 【Leetcode】Insert Delete GetRandom O(1) - Duplicates allowed
- leetcode之Insert Delete GetRandom O(1) - Duplicates allowed
- [leetcode] 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 151.Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- <leetcode>381. Insert Delete GetRandom O(1) - Duplicates allowed
- LeetCode Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed
- Insert Delete GetRandom O(1) - Duplicates allowed in JAVA
- LeetCode 381---Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- WEB架构师成长之路
- 第八周项目三(2)
- 腾讯视频引入取出广告
- 14
- 15
- Insert Delete GetRandom O(1) - Duplicates allowed
- 虚拟机的桥接模式和NAT模式的区别
- 微信第三方登录与静默授权
- React Webpack报错
- 第八周 项目四--字符串加密
- Lua和C++交互详细总结
- 蓝桥杯官网 入门训练 序列求和 JAVA
- 写好unit test的建议和例子
- 教大家如何在qualcomm平台移植MTP驱动程序