MCPcopy
hub / github.com/qiyuangong/leetcode / remove

Method remove

python/380_Insert_Delete_GetRandom.py:17–30  ·  view source on GitHub ↗
(self, val)

Source from the content-addressed store, hash-verified

15 return True
16
17 def remove(self, val):
18 if val not in self.num_to_idx:
19 return False
20
21 idx = self.num_to_idx[val]
22 last = self.num_list[-1]
23
24 # swap last elem to current spot so you can pop the end
25 self.num_list[idx] = last
26 self.num_list.pop()
27 self.num_to_idx[last] = idx
28 del self.num_to_idx[val]
29
30 return True
31
32 def getRandom(self):
33 return random.choice(self.num_list)

Callers

nothing calls this directly

Calls 1

popMethod · 0.45

Tested by

no test coverage detected