Python | Leetcode Python题解之第398题随机数索引
题目:
题解:
class Solution:def __init__(self, nums: List[int]):self.nums = numsdef pick(self, target: int) -> int:ans = cnt = 0for i, num in enumerate(self.nums):if num == target:cnt += 1 # 第 cnt 次遇到 targetif randrange(cnt) == 0:ans = ireturn ans