Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitb8d4ee3

Browse files
Update 380.py
1 parentcd769ac commitb8d4ee3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎001-500/380.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,37 @@ def remove(self, val: int) -> bool:
2626
defgetRandom(self)->int:
2727
returnrandom.choice(self.data)
2828

29+
30+
classRandomizedSet:
31+
32+
def__init__(self):
33+
self.memory= {}
34+
self.values= []
35+
36+
37+
definsert(self,val:int)->bool:
38+
ifvalinself.memory:
39+
returnFalse
40+
else:
41+
self.values.append(val)
42+
self.memory[val]=len(self.values)-1
43+
returnTrue
44+
2945

46+
defremove(self,val:int)->bool:
47+
ifvalnotinself.memory:
48+
returnFalse
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+
delself.memory[val]
55+
returnTrue
56+
57+
58+
defgetRandom(self)->int:
59+
returnrandom.choice(self.values)
3060

3161

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp