We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9d884ed commit 31a8860Copy full SHA for 31a8860
leetcode.com 380. Insert Delete GetRandom O(1)/main.py
@@ -0,0 +1,29 @@
1
+import random
2
+
3
4
+class RandomizedSet:
5
+ def __init__(self):
6
+ self.dt = {}
7
8
+ def insert(self, val: int) -> bool:
9
+ if val not in self.dt:
10
+ self.dt[val] = None
11
+ return True
12
13
+ return False
14
15
+ def remove(self, val: int) -> bool:
16
+ if val in self.dt:
17
+ del self.dt[val]
18
19
20
21
22
+ def getRandom(self) -> int:
23
+ return random.choice(list(self.dt.keys()))
24
25
+# Your RandomizedSet object will be instantiated and called as such:
26
+# obj = RandomizedSet()
27
+# param_1 = obj.insert(val)
28
+# param_2 = obj.remove(val)
29
+# param_3 = obj.getRandom()
0 commit comments