privatefinal Map<Integer, Integer> map; // key 为值,value 为 list 中的索引 privatefinal List<Integer> list; // 存储的 val 值
/** * Initialize your data structure here. */ publicRandomizedSet() { this.map = newHashMap<>(); this.list = newArrayList<>(); }
/** * Inserts a value to the set. Returns true if the set did not already contain the specified element. */ publicbooleaninsert(int val) { if (map.containsKey(val)) { returnfalse; }
/** * Removes a value from the set. Returns true if the set contained the specified element. */ publicbooleanremove(int val) { if (!map.containsKey(val)) { returnfalse; }