当前位置:网站首页>710. random numbers in the blacklist
710. random numbers in the blacklist
2022-06-26 14:48:00 【anieoo】
Original link :710. Random numbers in the blacklist

solution:
The title requires that a random one be returned 0 ~ n - 1 Numbers not on the blacklist , So we can 0 ~ n - 1 Divide into two groups .
The first group is 0 ~ n - len - 1 and The second group n - len ~ n - 1 among len Indicates the number of numbers in the blacklist .
Use a hash table to create 0 ~ n - len - 1 In the blacklist count to n - len ~ n - 1 White list number mapping in .
class Solution {
public:
int n,len; //len Save the length of the blacklist
unordered_map<int,int> hash; //hash Storage 0~n - len - 1 Medium blacklist number pairs n - len ~ n White list mapping in
Solution(int _n, vector<int>& blacklist) {
n = _n;
len = blacklist.size();
unordered_set<int> s;
for(int i = n - len;i < n;i++) s.insert(i);
for(auto &x : blacklist) s.erase(x); // preservation n - len ~ n In the white list
// Building mapping
auto it = s.begin();
for (auto x: blacklist)
if (x < n - len)
hash[x] = *it ++ ;
}
int pick() {
int x = rand() % (n - len);
if(hash.count(x)) return hash[x];
return x;
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(n, blacklist);
* int param_1 = obj->pick();
*/
边栏推荐
- Leaflet load day map
- Electron
- MHA高可用配合及故障切换
- Understand the difference and use between jsonarray and jsonobject
- Practical website recommendations worth collecting for College Students
- 秒懂JSONArray和JSONObject的区别和使用
- 这才是优美的文件系统挂载方式,亲测有效
- VIM auto fill auto indent explanation
- wptx64能卸载吗_win10自带的软件哪些可以卸载
- 在云服务器中云磁盘如何挂载
猜你喜欢
![[solo π] ADB connects multiple mobile phones](/img/44/4bd88725434fb67c9237645f024071.png)
[solo π] ADB connects multiple mobile phones

Leaflet load day map

印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网

Attention meets geometry: geometry guided spatiotemporal attention consistency self supervised monocular depth estimation

Mathematical modeling of war preparation 30 regression analysis 2

ArcGIS batch render layer script

Use abp Zero builds a third-party login module (I): Principles

人的顶级能量从哪里获取?

2022年最新贵州建筑八大员(机械员)模拟考试题库及答案

One article of the quantification framework backtrader read observer
随机推荐
JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了
ArcGIS batch render layer script
VMware partial settings
transformers DataCollatorWithPadding类
【使用yarn运行报错】The engine “node“ is incompatible with this module.
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
STM32F1和GD32F1有什么区别?
Informatics Olympiad 1405: sum and product of prime numbers (thinking problem)
从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭
这才是优美的文件系统挂载方式,亲测有效
登录认证服务
GDAL multiband synthesis tool
Sword finger offer 05.58 Ⅱ string
How to personalize VIM editor format (DIY)
From Celsius to the three arrows: encrypting the domino of the ten billion giants, and drying up the epic liquidity
Practical website recommendations worth collecting for College Students
Online bull Blogger
Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)
挖财注册开户安全吗,有没有什么风险?
【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器