Bootstrap

Golang | Leetcode Golang题解之第398题随机数索引

题目:

题解:

type Solution []int

func Constructor(nums []int) Solution {
    return nums
}

func (nums Solution) Pick(target int) (ans int) {
    cnt := 0
    for i, num := range nums {
        if num == target {
            cnt++ // 第 cnt 次遇到 target
            if rand.Intn(cnt) == 0 {
                ans = i
            }
        }
    }
    return
}
;