<Random> 380

380. Insert Delete GetRandom O(1)

class RandomizedSet {
    ArrayList<Integer> nums;
    HashMap<Integer, Integer> locs;
    Random rand = new Random();
    /** Initialize your data structure here. */
    public RandomizedSet() {
        nums = new ArrayList<Integer>();
        locs = 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) {
        boolean contain = locs.containsKey(val);
        if( contain ) return false;
        locs.put(val, nums.size());
        nums.add(val);
        return true;
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        boolean contain = locs.containsKey(val);
        if( !contain ) return false;
        int loc = locs.get(val);
        if(loc < nums.size() - 1){// not the last one than swap the last one with this val
            int lastOneVal = nums.get(nums.size() - 1);
            nums.set(loc, lastOneVal);
            locs.put(lastOneVal, loc);
        }
        locs.remove(val);
        nums.remove(nums.size() - 1);
        return true;
    }
    
    /** Get a random element from the set. */
    public int getRandom() {
        return nums.get(rand.nextInt(nums.size()));  
    }
}

猜你喜欢

转载自www.cnblogs.com/Afei-1123/p/11942101.html