当前位置:网站首页>力扣 710. 黑名单中的随机数
力扣 710. 黑名单中的随机数
2022-07-01 21:56:00 【三更鬼】
题目来源:https://leetcode.cn/problems/random-pick-with-blacklist/
大致题意:
给定一个数字范围 n,一个黑名单数组,设计一个类返回 0 到 n - 1 之间不在黑名单中的随机数,要求每个数被返回的概率应该相等
思路
设黑名单数组长度为 m,那么返回数字的个数有 n - m 个
所以可以设置随机数的 bound 为 n - m
- 若黑名单中的数都大于等于 bound,那么不用做任何处理
- 若黑名单中的数有小于 bound,可以使用哈希表将其映射到 [bound, n - 1] 中。这样在随机数生成器生成黑名单中小于 bound 的数时,直接映射到另一个数即可
代码:
public class Pick {
// 映射黑名单小于 bound 的数
Map<Integer, Integer> map;
int bound;
Random random;
public Pick(int n, int[] blacklist) {
map = new HashMap<>();
int m = blacklist.length;
// 初始化 bound
bound = n - m;
random = new Random();
// 存黑名单数字的哈希表
Set<Integer> blackSet = new HashSet<>();
for (int num : blacklist) {
if (num >= bound) {
blackSet.add(num);
}
}
// 映射索引
int idx = bound;
// 遍历黑名单
for (int num : blacklist) {
// 若该黑名单数字小于 bound,将其映射为 [bound, n - 1] 中不在黑名单且还未被映射的数
if (num < bound) {
// 若当前要映射的数在黑名单中,索引 +1
while (blackSet.contains(idx)) {
idx++;
}
map.put(num, idx++);
}
}
}
public int pick() {
// 生成随机数
int x = random.nextInt(bound);
// 返回随机数,若随机数在 map 中,返回其映射的数字
return map.getOrDefault(x, x);
}
}
边栏推荐
- 搜狗微信APP逆向(二)so层
- Digital currency: far-reaching innovation
- Map container
- 内部字段分隔符
- pytorch训练自己网络后可视化特征图谱的代码
- 每日刷题记录 (十)
- Metauniverse may become a new direction of Internet development
- Lc669. Prune binary search tree
- Turn -- bring it and use it: share a gadget for checking memory leaks
- SAP UI5 应用开发教程之一百零四 - SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
猜你喜欢

MySQL -- index of MyISAM storage engine
![[image segmentation] 2021 segformer neurips](/img/2f/a8631cbe9a46419b8dbd5205e1f5b5.png)
[image segmentation] 2021 segformer neurips

台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条

Hide the creation and use of users

Quantifiers of regular series

3DE resources have nothing or nothing wrong

激发新动能 多地发力数字经济

Appium automation test foundation - appium installation (I)

Use three JS realize the 'ice cream' earth, and let the earth cool for a summer

Yolov5.5 call local camera
随机推荐
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
转--原来gdb的底层调试原理这么简单
I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction
Favorite transaction code management tool in SAP GUI
Rust language - Introduction to Xiaobai 05
redis配置文件中常用配置详解[通俗易懂]
ECMAScript 2022 was officially released. Have you heard about it?
[QT widget] encapsulates a simple thread management class
tcpdump命令使用详解
[image segmentation] 2021 segformer neurips
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Friendly serial assistant tutorial_ How to configure friendly serial port debugging assistant - tutorial on using friendly serial port debugging assistant
A few minutes before work, I found out V-model and The difference between sync
转--深入LUA脚本语言,让你彻底明白调试原理
QStringList 的常规使用
聊一聊Zabbix都监控哪些参数
【c语言】malloc函数详解[通俗易懂]
Turn -- the underlying debugging principle of GDB is so simple
Yolov5.5 call local camera