Leetcode 381. Insert Delete GetRandom O(1) - Duplicates allowed 可重复常数增删改系统 解题报告
2016-09-17 16:19
447 查看
1 解题思想
这道题是380的进化版,其实也就是在插入的时候允许了重复的值,变的更加复杂了一些。这道题是基于这题来的,所以请先看下这题,我只说下如何从380过渡到381
Leetcode 380. Insert Delete GetRandom O(1) 常数增删改系统 解题报告
因为一个值要多个位置,所以原来的那个HashMap肯定要从<值-位置>,改成<值-位置的集合>
那么查询还是直接操作ArrayList没有难度
而插入的时候,是插入到位置集合也没有难度
问题在于删除。
删除的时候,idea和之前380一样,同样是将ArrayList的最后一个交换到前面,但是现在一个值有多个位置,所以我们要交换对位置,不能和原来一样直接替换。
所以根据值的集合选的不同,有所不同:
如你可以使用:
ArrayList来存储集合,那么你替换的时候需要遍历一下位置替换
我选择了Stack,那么就需要保证出栈的顺序有保障
或者其他更高级的快速的结构,随你,只要方便就好。。思想就是380的拓展,懂了380这个自然能变
2 原题
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();
3 AC解
public class RandomizedCollection { //用来索引下在ArrayList中的关系,做到快速定位 private HashMap<Integer,Stack<Integer>> locations; //存储原始数据 private ArrayList<Integer> list; private Random random; //为了排序,控制栈的 ArrayList<Integer> tmpForStack; /** Initialize your data structure here. */ public RandomizedCollection() { this.locations = new HashMap<Integer,Stack<Integer>>(); this.list = new ArrayList<Integer>(); this.random = new Random(); this.tmpForStack = new ArrayList<Integer>(); } /** * 直接插入 * */ public boolean insert(int val) { boolean flag = false; if( locations.containsKey(val) == false ){ locations.put(val,new Stack<Integer>()); } if(locations.get(val).isEmpty()) flag = true; locations.get(val).push(list.size()); list.add(val); return flag; } /** Removes a value from the set. Returns true if the set contained the specified element. */ /** * 核心思想在于:把删除的元素交换到ArrayList最后一位上,这样就可以O1完成了, * * 唯一的一点不同是,要保证在每一个元素对应的位置的栈,要保证其顺序是从小到大,每次出栈的一定是最大的,所以我这里交换List后,更新那个元素的栈时需要先把比他大的弹出来,再放回去 * */ public boolean remove(int val) { if( locations.containsKey(val) == false || locations.get(val).isEmpty() ) return false; int tmpLocation = locations.get(val).pop(); if(tmpLocation != list.size() - 1){ int lastVal = list.get(list.size() -1 ); list.set(tmpLocation,lastVal); locations.get(lastVal).pop(); tmpForStack.clear(); while(locations.get(lastVal).isEmpty() == false && locations.get(lastVal).peek() > tmpLocation){ tmpForStack.add(locations.get(lastVal).pop()); } locations.get(lastVal).push(tmpLocation); for(int tval:tmpForStack) locations.get(lastVal).push(tval); } list.remove(list.size() - 1); return true; } /** Get a random element from the set. */ public int getRandom() { return list.get(random.nextInt(list.size())); } } /** * Your RandomizedCollection object will be instantiated and called as such: * RandomizedCollection obj = new RandomizedCollection(); * boolean param_1 = obj.insert(val); * boolean param_2 = obj.remove(val); * int param_3 = obj.getRandom(); */
相关文章推荐
- Leetcode 380. Insert Delete GetRandom O(1) 常数增删改系统 解题报告
- [Leetcode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 解题报告
- [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复
- [LeetCode]Remove Duplicates from Sorted List解题报告
- leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法
- Leetcode 357. Count Numbers with Unique Digits 统计没有重复数字的数 解题报告
- leetcode解题报告(18):Contains Duplicate
- [Leetcode] 26. Remove Duplicates from Sorted Array 解题报告
- leetcode:Remove Duplicates from Sorted Array(去掉数组重复数字,常数空间限制)【面试算法题】
- 【LeetCode】Remove Duplicates from Sorted Array 解题报告
- Leetcode 82. Remove Duplicates from Sorted List II 移除重复 链表版-2 解题报告
- leetcode解题报告(6):Remove Duplicates from Sorted List
- LeetCode-Contains Duplicate III-解题报告
- [leetcode] 26. Remove Duplicates from Sorted Array 解题报告
- Leetcode 395. Longest Substring with At Least K Repeating Characters 至少K重复最长子序列 解题报告
- [LeetCode] Remove Duplicates from Sorted Array II 解题报告
- LeetCode-Contains Duplicate II-解题报告
- [LeetCode] Remove Duplicates from Sorted Array II 解题报告
- [LeetCode] Remove Duplicates from Sorted Array 解题报告
- leetcode解题报告(1):Remove Duplicates from Sorted Array