当前位置:网站首页>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;
}
};
边栏推荐
- 在Oracle中update大量数据会不会导致undo空间爆掉
- Expand and collapse too high div
- Matrix fast power notes
- SQL index learning notes
- mysql性能監控和sql語句
- Redis (IV) redis association table caching
- 挖财商学院证券开户安全嘛?
- Oracle11g 启动数据库时报错 ORA-27154: post/wait create failed
- 2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告
- Swiftui development experience: data layer of application design for offline priority
猜你喜欢

See how I store integer data in the map < string, string > set

Concise course of probability theory and statistics in engineering mathematics second edition review outline

MySQL 10th job - View

That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade

Grain Mall - distributed Foundation

DataBinding使用与原理分析

ISO 26262之——2功能安全概念

Redis (basic) - learning notes

3、 Linked list exercise

4、 Stacks and queues
随机推荐
Developers, what is the microservice architecture?
consul微服务治理中心踩坑
MySQL 30 military regulations
近期工作汇报
互联网对抗神器之漏洞扫描与反渗透
【北邮果园微处理器设计】10 Serial Communication 串口通信笔记
Server single and two-way adjustable one key mutual trust script!
Adaptiveavgpool2d does not support onnx export. Customize a class to replace adaptiveavgpool2d
DataBinding使用与原理分析
Expand and collapse too high div
2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告
Alibaba cloud OSS - object storage service (tool)
2020.7.6 interview with fence network technology company
laravel 安装报错 Uncaught ReflectionException: Class view does not exist
[work details] March 18, 2020
Oracle11g 启动数据库时报错 ORA-27154: post/wait create failed
Cereals Mall - Distributed Advanced
Notes - simple but adequate series_ KVM quick start
JWT certification agreement -- I opened a Yihong hospital
Reasons for "unresolved external symbols" during vs or QT compilation link: