题目:
题解:
class Solution:
def __init__(self, nums: List[int]):
self.nums = nums
def pick(self, target: int) -> int:
ans = cnt = 0
for i, num in enumerate(self.nums):
if num == target:
cnt += 1 # 第 cnt 次遇到 target
if randrange(cnt) == 0:
ans = i
return ans