-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedCollection.java
More file actions
56 lines (51 loc) · 1.71 KB
/
Copy pathRandomizedCollection.java
File metadata and controls
56 lines (51 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package leetcode;
import java.util.*;
public class RandomizedCollection {
Map<Integer, Set<Integer>> map;
List<Integer> list;
/** Initialize your data structure here. */
public RandomizedCollection() {
map = new HashMap<>();
list = new ArrayList();
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
list.add(val);
Set<Integer> set;
if (map.containsKey(val)) {
set = map.get(val);
set.add(list.size() - 1);
return false;
} else {
set = new LinkedHashSet<>();
set.add(list.size() - 1);
map.put(val, set);
return true;
}
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
if (map.containsKey(val)) {
Set<Integer> set = map.get(val);
Integer index = set.iterator().next();
set.remove(index);
if (set.isEmpty()) {
map.remove(val);
}
if (index < list.size() - 1) {
Set<Integer> lastSet = map.get(list.get(list.size() - 1));
list.set(index, list.get(list.size() - 1));
lastSet.remove(list.size() - 1);
lastSet.add(index);
}
list.remove(list.size() - 1);
return true;
} else {
return false;
}
}
/** Get a random element from the collection. */
public int getRandom() {
return list.get(new Random().nextInt(list.size()));
}
}