-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
59 lines (53 loc) · 1.81 KB
/
Main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class InsertDeleteGetRandom380 {
class RandomizedSet {
public Random rand;
public ArrayList<Integer> list;
public int last;
public HashMap<Integer, Integer> map; // val : index
/** Initialize your data structure here. */
public RandomizedSet() {
this.rand = new Random();
this.list = new ArrayList<>();
this.map = new HashMap<>();
this.last = 0;
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if (this.map.containsKey(val)) {
return false;
}
this.map.put(val, this.last);
if (this.last >= this.list.size()) {
this.list.add(val);
this.last = this.list.size();
} else {
this.list.set(this.last++, val);
}
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
if (!this.map.containsKey(val)) {
return false;
}
int index = this.map.get(val);
this.map.remove(val);
int replaced = this.list.get(this.last - 1);
if (replaced != val) {
this.list.set(index, replaced);
this.map.put(replaced, index);
}
this.last--;
return true;
}
/** Get a random element from the set. */
public int getRandom() {
return this.list.get(
this.rand.nextInt(this.last)
);
}
}
}