-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path398.py
45 lines (35 loc) · 1.11 KB
/
398.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 猴子
class Solution:
def __init__(self, nums: List[int]):
self.nums = nums
self.length = len(nums)
def pick(self, target: int) -> int:
ans = randint(0, self.length-1)
while self.nums[ans] != target:
ans = randint(0, self.length-1)
return ans
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)
# 哈希表
class Solution:
def __init__(self, nums: List[int]):
self.hashtable = defaultdict(list)
for i, val in enumerate(nums):
self.hashtable[val].append(i)
def pick(self, target: int) -> int:
return choice(self.hashtable[target])
# 水塘抽样
#速度上明显更快,内存消耗上也与猴子相当
class Solution:
def __init__(self, nums: List[int]):
self.nums = nums
def pick(self, target: int) -> int:
ans, cnt = 0, 0
for i, val in enumerate(self.nums):
if val == target:
x = randint(0, cnt)
if not x:
ans = i
cnt += 1
return ans