380. Insert Delete GetRandom O(1) Medium
1/**
2 * [380] Insert Delete GetRandom O(1)
3 *
4 * Implement the RandomizedSet class:
5 *
6 * RandomizedSet() Initializes the RandomizedSet object.
7 * bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
8 * bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
9 * int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.
10 *
11 * You must implement the functions of the class such that each function works in average O(1) time complexity.
12 *
13 * Example 1:
14 *
15 * Input
16 * ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
17 * [[], [1], [2], [2], [], [1], [2], []]
18 * Output
19 * [null, true, false, true, 2, true, false, 2]
20 * Explanation
21 * RandomizedSet randomizedSet = new RandomizedSet();
22 * randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
23 * randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
24 * randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
25 * randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
26 * randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
27 * randomizedSet.insert(2); // 2 was already in the set, so return false.
28 * randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
29 *
30 *
31 * Constraints:
32 *
33 * -2^31 <= val <= 2^31 - 1
34 * At most 2 * 10^5 calls will be made to insert, remove, and getRandom.
35 * There will be at least one element in the data structure when getRandom is called.
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/insert-delete-getrandom-o1/
41// discuss: https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45struct RandomizedSet {
46 false
47 }
48
49
50/**
51 * `&self` means the method takes an immutable reference.
52 * If you need a mutable reference, change it to `&mut self` instead.
53 */
54impl RandomizedSet {
55
56 fn new() -> Self {
57
58 }
59
60 fn insert(&self, val: i32) -> bool {
61
62 }
63
64 fn remove(&self, val: i32) -> bool {
65
66 }
67
68 fn get_random(&self) -> i32 {
69
70 }
71}
72
73/**
74 * Your RandomizedSet object will be instantiated and called as such:
75 * let obj = RandomizedSet::new();
76 * let ret_1: bool = obj.insert(val);
77 * let ret_2: bool = obj.remove(val);
78 * let ret_3: i32 = obj.get_random();
79 */
80
81// submission codes end
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_380() {
89 }
90}
91
Back
© 2025 bowen.ge All Rights Reserved.