当前位置:网站首页>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();
*/
边栏推荐
- Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
- (improved) bubble sorting and (improved) cocktail sorting
- Login authentication service
- fileinput.js php,fileinput
- Transformers datacollatorwithpadding class
- 【soloπ】adb连接单个多个手机
- 710. 黑名单中的随机数
- Equation derivation: second order active bandpass filter design! (download: Tutorial + schematic + Video + code)
- 量化框架backtrader之一文读懂observer观测器
- Unity uses skybox panoramic shader to make panorama preview. There is a gap. Solution
猜你喜欢
Where do people get their top energy?
Deploy the flask environment using the pagoda panel
Sword finger offer 45.61 Sort (simple)
Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭
View触摸分析
权威发布 | 延安大学2022年教师岗位招聘公告
赠书 | 《认知控制》:我们的大脑如何完成任务?
Leaflet load day map
Sword finger offer 05.58 Ⅱ string
随机推荐
Oracle11g database import and export method tutorial [easy to understand]
券商经理给的开户链接办理股票开户安全吗?我想开个户
Extended hooks
This is the graceful file system mounting method, which is effective through personal testing
Two point answer, 01 score planning (mean / median conversion), DP
BM3D in popular language
Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
Complete diagram / Euler loop
GDAL and opencv smooth and blur TIF images
NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...
Is the account opening link given by the broker manager safe? Who can I open an account with?
Mathematical modeling of war preparation 30 regression analysis 2
Chinese output of PostGIS console is garbled
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
Combat readiness mathematical modeling 32 correlation analysis 2
【使用yarn运行报错】The engine “node“ is incompatible with this module.
Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
房东拿租金去还房贷是天经地义的嘛
Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
Matplotlib common operations