当前位置:网站首页>Don't insist on 66 days. Weight generates random numbers
Don't insist on 66 days. Weight generates random numbers
2022-07-29 04:16:00 【A little Ming】

class Solution {
int[] pre;
int total;
public Solution(int[] w) {
pre = new int[w.length];
pre[0] = w[0];
for (int i = 1; i < w.length; ++i) {
pre[i] = pre[i - 1] + w[i];
}
total = Arrays.stream(w).sum();
}
public int pickIndex() {
int x = (int) (Math.random() * total) + 1;
return binarySearch(x);
}
private int binarySearch(int x) {
int low = 0, high = pre.length - 1;
while (low < high) {
int mid = (high - low) / 2 + low;
if (pre[mid] < x) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
}
author :LeetCode-Solution
link :https://leetcode.cn/problems/cuyjEf/solution/an-quan-zhong-sheng-cheng-sui-ji-shu-by-bosxd/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .边栏推荐
- Deep learning training strategy -- warming up the learning rate
- Problems encountered in vscode connection SSH
- 15.federation
- How to set the SQL execution timeout for flick SQL
- 14. Haproxy+kept load balancing and high availability
- MySQL gets the maximum value record by field grouping
- SVG--loading动画
- Design of environment detection system based on STM32 and Alibaba cloud
- C language - character array - string array - '\0' -sizeof-strlen() -printf()
- Why are there so many unknowns when opengauss starts?
猜你喜欢
随机推荐
Openfeign asynchronous call problem
SVG--loading动画
[Openstack] keystone,nova
How to query the submission number of a version
Jenkins 参数化构建中 各参数介绍与示例
伏英娜:元宇宙就是新一代互联网!
Some problems about pointers
Fu Yingna: Yuan universe is the new generation of Internet!
Rhel8 patch package production
Install the laser of ROS_ scan_ Problems encountered in match library (I)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
BIO、NIO、AIO的区别和原理
Why do I delete the original record (OP d) and then add a new one in Kafka when I update MySQL data
[kvm] common commands
C语言:浅谈各种复杂的声明
Pix2.4.8 from start to installation (2021.4.4)
[kvm] install KVM
11.备份交换机
LCA board
Taobao product details interface (product details page data interface)








