当前位置:网站首页>LeetCode 710 黑名单中的随机数[随机数] HERODING的LeetCode之路
LeetCode 710 黑名单中的随机数[随机数] HERODING的LeetCode之路
2022-06-26 10:09:00 【HERODING23】

解题思路:
实现[0,n-m]区间黑名单对[n-m, n]的非黑名单映射,然后取[0,n-m]随机数,如果是白名单直接返回,否则返回映射的白名单数,代码如下:
class Solution {
unordered_map<int, int> b2w;
int bound;
public:
Solution(int n, vector<int> &blacklist) {
int m = blacklist.size();
bound = n - m;
unordered_set<int> black;
for (int b: blacklist) {
if (b >= bound) {
black.emplace(b);
}
}
int w = bound;
for (int b: blacklist) {
if (b < bound) {
while (black.count(w)) {
++w;
}
b2w[b] = w++;
}
}
}
int pick() {
int x = rand() % bound;
return b2w.count(x) ? b2w[x] : x;
}
};
边栏推荐
- Update mysql5.6 to 5.7 under Windows
- Using baijiafan to automatically generate API calls: progress in JS (II)
- (Typora图床)阿里云oss搭建图床+Picgo上传图片详细教程
- SQL Server foundation introduction collation
- 挖财商学院证券开户安全嘛?
- laravel-admin隐藏按钮, 及设置按钮显示, 默认序列, form 表单的不可修改值
- 3、 Linked list exercise
- sysbench基础介绍
- Work report (2)
- sliding window
猜你喜欢

Which PHP open source works deserve attention
![[Beiyou orchard microprocessor design] 10 serial communication serial communication notes](/img/61/b4cfb0500bbe39ed6a371bb8672a2f.png)
[Beiyou orchard microprocessor design] 10 serial communication serial communication notes

Developers, what is the microservice architecture?

MySQL 8th job

SVN 安装配置

Sqli labs range 1-5

Win10 start FTP service and set login authentication

Quantitative investment learning - Introduction to classic books

哪些PHP开源作品值得关注

Opencv image processing - grayscale processing
随机推荐
DataBinding使用与原理分析
Is it safe to use flush mobile phones to speculate in stocks? How to fry stocks with flush
laravel 安装报错 Uncaught ReflectionException: Class view does not exist
RDB persistence validation test
搜索引擎高级搜索方法记录
Plookup table in appliedzkp zkevm (8)
Win10 start FTP service and set login authentication
24 个必须掌握的数据库面试问题!
JWT (SSO scheme) + three ways of identity authentication
Svn installation configuration
[work details] March 18, 2020
Fabric. JS upper dash, middle dash (strikethrough), underline
ISO 26262之——2功能安全概念
mysql性能监控和sql语句
最牛X的CMDB系统
AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
Expand and collapse too high div
ACK攻击是什么意思?ACK攻击怎么防御?
Character sets and comparison rules
2、 Linear table