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

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

示例:

type Solution struct {
    n []int
}


func Constructor(nums []int) Solution {
    var s *Solution= new(Solution)
    s.n = nums
    return *s
}


func (this *Solution) Pick(target int) int {
    nums := this.n
    result := -1
    count := 0
    for i:= 0; i<len(nums) ; i++{
        if nums[i] != target{
            continue
        }
        
        if count++;rand.Intn(count)==0{
            result = i
        }
    }
    return result
}


/**
 * Your Solution object will be instantiated and called as such:
 * obj := Constructor(nums);
 * param_1 := obj.Pick(target);
 */

解题思路:水塘抽样 Reservoir sampling

遗留问题:1.水塘抽样具体原理已经不记得了,以前学过相关知识

2.stuct用法