algo/di-yi-zhan-da78c/shou-ba-sh-48c1d/dai-quan-z-585d6/ #1506
Replies: 3 comments
-
golang代码没翻译,我给个版本 type Solution struct {
preSum []int
}
func Constructor(w []int) Solution {
n:=len(w)
preSum:=make([]int,n+1)
for i:=0;i<n;i++{
preSum[i+1]=preSum[i]+w[i]
}
return Solution{preSum:preSum}
}
func (this *Solution) PickIndex() int {
n:=len(this.preSum)
total := this.preSum[len(this.preSum)-1]
random:=rand.Intn(total)+1
index:=sort.Search(n,func(i int)bool {return this.preSum[i]>=random})
return index-1
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
总权重是7份,各自占1/7,2/7,3/7,1/7,应该是[0,7]的闭区间啊,为啥是【1,7】的闭区间,如果是[1,7]的闭区间岂不是说第一个数字被选到的概率为无限接近于0? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
algo/di-yi-zhan-da78c/shou-ba-sh-48c1d/dai-quan-z-585d6/
Info 数据结构精品课 (https://aep.h5.xeknow.com/s/1XJHEO) 和 递归算法专题课 (https://aep.xet.tech/s/3YGcq3) 限时附赠网站会员,全新纸质书《labuladong 的算法笔记》 (https://labuladong.gitee.io/algo/images/book/book_i...
https://labuladong.github.io/algo/di-yi-zhan-da78c/shou-ba-sh-48c1d/dai-quan-z-585d6/
Beta Was this translation helpful? Give feedback.
All reactions