Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8d4ee3

Browse filesBrowse files
Update 380.py
1 parent cd769ac commit b8d4ee3
Copy full SHA for b8d4ee3

File tree

1 file changed

+30
-0
lines changed
Filter options

1 file changed

+30
-0
lines changed

‎001-500/380.py

Copy file name to clipboardExpand all lines: 001-500/380.py
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,37 @@ def remove(self, val: int) -> bool:
2626
def getRandom(self) -> int:
2727
return random.choice(self.data)
2828

29+
30+
class RandomizedSet:
31+
32+
def __init__(self):
33+
self.memory = {}
34+
self.values = []
35+
36+
37+
def insert(self, val: int) -> bool:
38+
if val in self.memory:
39+
return False
40+
else:
41+
self.values.append(val)
42+
self.memory[val]=len(self.values)-1
43+
return True
44+
2945

46+
def remove(self, val: int) -> bool:
47+
if val not in self.memory:
48+
return False
49+
else:
50+
val_pos = self.memory[val]
51+
self.memory[self.values[-1]] = val_pos
52+
self.values[val_pos], self.values[-1] = self.values[-1], self.values[val_pos]
53+
self.values.pop()
54+
del self.memory[val]
55+
return True
56+
57+
58+
def getRandom(self) -> int:
59+
return random.choice(self.values)
3060

3161

3262
# Your RandomizedSet object will be instantiated and called as such:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.