给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引。 您可以假设给定的数字一定存在于数组中。

注意
数组大小可能非常大。 使用太多额外空间的解决方案将不会通过测试。

示例:

  1. int[] nums = new int[] {1,2,3,3,3};
  2. Solution solution = new Solution(nums);
  3. // pick(3) 应该返回索引 2,3 或者 4。每个索引的返回概率应该相等。
  4. solution.pick(3);
  5. // pick(1) 应该返回 0。因为只有nums[0]等于1。
  6. solution.pick(1);

题解

最典型的蓄水池抽样

class Solution:

    def __init__(self, nums: List[int]):
        self.nums = nums

    def pick(self, target: int) -> int:
        cnt = 0
        ans = None
        for i in range(len(self.nums)):
            if self.nums[i] == target:
                cnt += 1
                if random.randint(1, cnt) == cnt:
                    ans = i
        return ans