奇怪的是蓄水池抽样算法无法AC 代码2是蓄水池抽样
class Solution(object):
import random
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.n=nums
def pick(self, target):
"""
:type target: int
:rtype: int
"""
res=[]
for i in range(len(self.n)):
if self.n[i]==target:
res.append(i)
return random.choice(res)
class Solution(object):
import random
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.n=nums
def pick(self, target):
"""
:type target: int
:rtype: int
"""
seed=0
for i in range(len(self.n)):
if self.n[i]==target:
if random.randint(0,seed)==0:
res=i
seed+=1
return res