Insert Delete GetRandom O(1) - Duplicates allowed
题目地址:
https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/description/
题目:
这道题可以用一个list来存数据,用map来存index和value的key-value pair。我们有两种选择,但是这里要注意我们要实现remove,所以用value来作为key是相对合理的,可以在O(1)时间找到该数字对应的index的集合。这里比较tricky的是当要remove的元素不是list最后一个,我们最好先把它和最后元素交换一下。
代码:
https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/description/
题目:
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();解题思路:
这道题可以用一个list来存数据,用map来存index和value的key-value pair。我们有两种选择,但是这里要注意我们要实现remove,所以用value来作为key是相对合理的,可以在O(1)时间找到该数字对应的index的集合。这里比较tricky的是当要remove的元素不是list最后一个,我们最好先把它和最后元素交换一下。
代码:
private List<Integer> nums; private Map<Integer, Set<Integer>> map; /** Initialize your data structure here. */public RandomizedCollection() { nums = new ArrayList<>(); map = new HashMap<>(); } /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */public boolean insert(int val) { boolean rst = map.containsKey(val); if(!rst){ map.put(val, new LinkedHashSet<>()); } nums.add(val); map.get(val).add(nums.size() - 1); return !rst; } /** Removes a value from the collection. Returns true if the collection contained the specified element. */public boolean remove(int val) { if(!map.containsKey(val)){ return false; } // swap the last element with the element we are about to remove if(!map.get(val).contains(nums.size() - 1)){ int removeIndex = map.get(val).iterator().next(); map.get(val).add(nums.size() - 1); map.get(val).remove(removeIndex); int lastVal = nums.get(nums.size() - 1); map.get(lastVal).remove(nums.size() - 1); map.get(lastVal).add(removeIndex); nums.set(removeIndex, lastVal); } map.get(val).remove(nums.size() - 1); nums.remove(nums.size() - 1); if(map.get(val).size() == 0){ map.remove(val); } return true; } /** Get a random element from the collection. */public int getRandom() { Random random = new Random(); int index = random.nextInt(nums.size()); return nums.get(index); }

Comments
Post a Comment