题目描述:
设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构。
注意: 允许出现重复元素。
insert(val):向集合中插入元素 val。
remove(val):当 val 存在时,从集合中移除一个 val。
getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。
示例:
// 初始化一个空的集合。
RandomizedCollection collection = new RandomizedCollection();
// 向集合中插入 1 。返回 true 表示集合不包含 1 。
collection.insert(1);
// 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。
collection.insert(1);
// 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。
collection.insert(2);
// getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
collection.getRandom();
// 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。
collection.remove(1);
// getRandom 应有相同概率返回 1 和 2 。
collection.getRandom();
用list保存所有出现的数字,用map保存的是次数
class RandomizedCollection {
// list用于最后的随机产生
List<Integer> list;
Map<Integer,Integer> map;
/** Initialize your data structure here. */
public RandomizedCollection() {
list = new ArrayList<>();
map = new HashMap<>();
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
if(!map.containsKey(val)){
map.put(val, 1);
list.add(val);
return true;
}else {
map.put(val, map.getOrDefault(val, 0) + 1);
list.add(val);
return false;
}
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
if(map.containsKey(val) && map.get(val) >= 1){
map.put(val, map.get(val) - 1);
list.remove(new Integer(val));
return true;
}else {
return false;
}
}
/** Get a random element from the collection. */
public int getRandom() {
System.out.println(list.size());
return list.get(new Random().nextInt(list.size()));
}
}
看看排名靠前的代码:
class RandomizedCollection {
private Map<Integer, Set<Integer>> map = new HashMap<>();
private Map<Integer, Integer> reversed = new HashMap<>();
private Random random = new Random(System.currentTimeMillis());
/** Initialize your data structure here. */
public RandomizedCollection() {
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
if (map.containsKey(val) && !map.get(val).isEmpty()) {
map.get(val).add(reversed.size());
reversed.put(reversed.size(), val);
return false;
} else {
Set<Integer> s = new HashSet<>();
s.add(reversed.size());
map.put(val, s);
reversed.put(reversed.size(), val);
return true;
}
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
boolean exists = false;
if (map.containsKey(val) && !map.get(val).isEmpty()) {
exists = true;
if (reversed.get(reversed.size() - 1) == val) {
// delete last one is enough
map.get(val).remove(reversed.size() - 1);
reversed.remove(reversed.size() - 1);
} else {
Set<Integer> valPoses = map.get(val);
int pos = -1;
for (Integer i : valPoses) {
pos = i;
break;
}
valPoses.remove(pos);
int lastsPos = reversed.size() - 1;
Integer last = reversed.remove(lastsPos);
reversed.put(pos, last);
// update `last`'s pos
Set<Integer> set = map.get(last);
set.remove(lastsPos);
map.get(last).add(pos);
map.get(last).remove(lastsPos);
}
}
return exists;
}
/** Get a random element from the collection. */
public int getRandom() {
return reversed.get(random.nextInt(reversed.size()));
}
}