当前位置:网站首页>不会就坚持66天吧 权重生成随机数
不会就坚持66天吧 权重生成随机数
2022-07-29 04:13:00 【一只小小明】

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;
}
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/cuyjEf/solution/an-quan-zhong-sheng-cheng-sui-ji-shu-by-bosxd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。边栏推荐
- 基于STM32和阿里云的环境检测系统设计
- 这个报错是什么鬼啊,不影响执行结果,但是在执行sql时一直报错。。。连接maxComputer是使用
- C语言实现三子棋游戏(详解)
- SQL server当存储过程接收的参数是int类型时,如何做判断?
- "Weilai Cup" 2022 Niuke summer multi school training camp 2H
- 优炫数据库有办法查到主集群每天传给备集群的日志量吗?
- Pointer of pointer???...
- Note: restframe work records many to one tables, how to serialize in that table (reverse query)
- Svg -- loading animation
- Array as function parameter -- pointer constant / constant pointer
猜你喜欢

Nacos registry

SQL window function
![[原理] 横向渗透的几种方式](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)
[原理] 横向渗透的几种方式

基于STM32和阿里云的环境检测系统设计

通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值

Code or script to speed up the video playback of video websites

Class starts! See how smardaten decomposes complex business scenarios

初识C语言(3)

Data mining -- code implementation of association analysis example (Part 2)

RMAN do not mark expired backups
随机推荐
Applet: Area scrolling, pull-down refresh, pull-up load more
The table of antd hides the pager when there is only one page
Function pointer and callback function
Summary on the thought of double pointer
如何查询版本的提交号
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation
[kvm] common commands
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
Openfeign asynchronous call problem
有一种密码学专用语言叫做ASN.1
Don't the JDBC SQL connector of the big guys Flink now support all databases, such as vertica?
Do you have a boss to help me check whether the parameter configuration of the Flink SQL connection Kafka authentication Kerberos is wrong
[kvm] create virtual machine from kickstart file
flink-sql 如何设置 sql执行超时时间
C language: getchar () and cache
Wechat applet monitors sliding events on the screen
Data mining -- Introduction to the basis of association analysis (Part 1)
【深度学习CPU(番外篇)——虚拟内存】
顺序表和链表
LCA 板子