当前位置:网站首页>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();
*/
边栏推荐
- datasets Dataset类(2)
- Use abp Zero builds a third-party login module (II): server development
- 工作上对金额价格类小数点的总结以及坑
- Is the account opening link given by the broker manager safe? Who can I open an account with?
- IP certificate application process of sectigo
- Combat readiness mathematical modeling 32 correlation analysis 2
- Where do people get their top energy?
- Leaflet load day map
- Online bull Blogger
- 使用 Abp.Zero 搭建第三方登录模块(二):服务端开发
猜你喜欢

Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type

vmware部分设置

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

获取两个dataframe的交并差集

From Celsius to the three arrows: encrypting the domino of the ten billion giants, and drying up the epic liquidity

One article of the quantification framework backtrader read observer

Stream常用操作以及原理探索

Jianzhi offer 43.47.46.48 dynamic planning (medium)

MySQL主从复制与读写分离

Authoritative announcement on the recruitment of teachers in Yan'an University in 2022
随机推荐
[solo π] ADB connects multiple mobile phones
Mark: unity3d cannot select resources in the inspector, that is, project locking
Leaflet loading ArcGIS for server map layers
Summary of decimal point of amount and price at work and pit
Numpy基本使用
1075 pat judge (25 points)
datasets Dataset类(2)
Numpy basic use
Chinese output of PostGIS console is garbled
年薪50万是一条线,年薪100万又是一条线…...
Jianzhi offer 43.47.46.48 dynamic planning (medium)
Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type
【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器
Question bank and answers of the latest Guizhou construction eight (Mechanics) simulated examination in 2022
STM32F1和GD32F1有什么区别?
从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭
Is the account opening link given by the broker manager safe? Who can I open an account with?
710. 黑名单中的随机数
工作上对金额价格类小数点的总结以及坑
MHA高可用配合及故障切换