题目描述
实现RandomizedSet 类:
RandomizedSet() 初始化 RandomizedSet 对象
bool insert(int val) 当元素 val 不存在时,向集合中插入该项,并返回 true ;否则,返回 false 。
bool remove(int val) 当元素 val 存在时,从集合中移除该项,并返回 true ;否则,返回 false 。
int getRandom() 随机返回现有集合中的一项(测试用例保证调用此方法时集合中至少存在一个元素)。每个元素应该有 相同的概率 被返回。
你必须实现类的所有函数,并满足每个函数的 平均 时间复杂度为 O(1) 。
代码
class RandomizedSet {
Set<Integer> set;
public RandomizedSet() {
set = new HashSet<>();
}
public boolean insert(int val) {
if(set.contains(val)) return false;
else{
set.add(val);
return true;
}
}
public boolean remove(int val) {
if(set.contains(val)){
set.remove(val);
return true;
}else return false;
}
public int getRandom() {
List<Integer> list = new ArrayList<>(set);
int n = list.size();
int id = (int)(Math.random()*n);
return list.get(id);
}
}
/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/