-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path380_Insert_Delete_GetRandom_O(1).java
46 lines (38 loc) · 1.17 KB
/
380_Insert_Delete_GetRandom_O(1).java
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
class RandomizedSet {
private final Random random = new Random();
private final Map<Integer, Integer> map = new HashMap<>();
private int[] vals = new int[32];
private int i = 0;
public RandomizedSet() {
}
public boolean insert(int val) {
Integer added = map.putIfAbsent(val, i);
if (added != null) return false;
if (i >= vals.length) {
vals = Arrays.copyOf(vals, vals.length * 2);
}
vals[i++] = val;
return true;
}
public boolean remove(int val) {
Integer removed = map.remove(val);
if (removed == null) return false;
if (removed < i - 1) {
vals[removed] = vals[i-1];
map.put(vals[i-1], removed);
}
i--;
return true;
}
public int getRandom() {
int index = random.nextInt(i);
return vals[index];
}
}
/**
* 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();
*/