380. Insert Delete GetRandom O(1) 设计数据结构:在1的时间内插入、删除、产生随机数

Implement the RandomizedSet class:

  • bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
  • bool remove(int val) Removes an item from the set if present. Returns if the item was present, false otherwise.
  • int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.

Follow up: Could you implement the functions of the class with each function works on average O(1) time?

 

Example 1:

Input
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
Output
[null, true, false, true, 2, true, false, 2]

Explanation
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
randomizedSet.insert(2); // 2 was already in the set, so return false.
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.

思路:不知道为啥要用map,因为添加的过程中不能重复

如果list中被删除的最后一个元素不是当前的val。就是本来不该删它的,现在把last放到它该放的地方indexInMap

就把list中最后一个元素设置在indexInMap位
map也把最后一个元素设置在indexInMap位

 

list应该用.size()

 

🌰:

 

RandomizedSet
insert 1
remove 2
insert 2
getRandom
remove 1
insert 2
getRandom

 

 

 

 

class RandomizedSet {
    List<Integer> list = new ArrayList<Integer>();
    Random rnd = new Random();
    HashMap<Integer, Integer> map = 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) {
        //如果已经有重复的,就return false
        if(map.containsKey(val)) {
            return false;
        }
        
        map.put(val, list.size());
        list.add(val);
        return true;
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        //如果根本没有这个元素,就return false
        if(!map.containsKey(val)) {
            return false;
        }
        
        //把list最后一位元素找出来,如果不是val,把最后一位元素放在该放的地方
        int lastInList = list.remove(list.size() - 1);
        int indexInMap = map.remove(val);
        
        if (lastInList != val) {
            map.put(lastInList, indexInMap);
            list.set(indexInMap, lastInList);
        }
        
        return true;
    }
    
    /** Get a random element from the set. */
    public int getRandom() {
        return list.get(rnd.nextInt(list.size()));
    }
}
View Code

 



posted @ 2020-09-17 11:00  苗妙苗  阅读(197)  评论(0编辑  收藏  举报