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.

  1. insert(val): Inserts an item val to the collection.
  2. remove(val): Removes an item val from the collection if present.
  3. 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