当前位置:网站首页>710. 黑名单中的随机数
710. 黑名单中的随机数
2022-06-26 13:56:00 【anieoo】
原题链接:710. 黑名单中的随机数

solution:
题目要求返回随机的一个0 ~ n - 1不在黑名单中的数字,因此可以把0 ~ n - 1分成两组。
第一组为0 ~ n - len - 1 和 第二组 n - len ~ n - 1 其中len表示黑名单的数字个数.
利用哈希表建立0 ~ n - len - 1中黑名单数到n - len ~ n - 1中白名单数字的映射。
class Solution {
public:
int n,len; //len保存黑名单的长度
unordered_map<int,int> hash; //hash存储0~n - len - 1中黑名单数对n - len ~ n中白名单的映射
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); //保存n - len ~ n中白名单
//建立映射
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();
*/
边栏推荐
- Atcoder 238
- 数学建模经验分享:国赛美赛对比/选题参考/常用技巧
- JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了
- Oracle ASMM和AMM
- [hnoi2010] flying sheep
- C language | Consortium
- Introduction to basic knowledge of C language (Daquan) [suggestions collection]
- 人的顶级能量从哪里获取?
- NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...
- 量化框架backtrader之一文读懂observer观测器
猜你喜欢

Related knowledge of libsvm support vector machine

Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient

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

How to personalize VIM editor format (DIY)

Electron

永远不要使用Redis过期监听实现定时任务!

Freefilesync folder comparison and synchronization software

PostGIS create spatial database

New specification of risc-v chip architecture

登录认证服务
随机推荐
【async/await】--异步编程最终解决方案
Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
Codeforces Round #765 (Div. 2) D. Binary Spiders
Oracle ASMM and AMM
Sword finger offer 21.57.58 I Double pointer (simple)
Sword finger offer 09.30 Stack
NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...
Understand the difference and use between jsonarray and jsonobject
Recent important news
idea快捷键
Two point answer, 01 score planning (mean / median conversion), DP
Electron
Introduction to granular computing
Sword finger offer 05.58 Ⅱ string
A remove the underline from the label
量化框架backtrader之一文读懂observer观测器
大学生值得珍藏的实用网站推荐
C language | file operation and error prone points
服务器创建虚拟环境跑代码
在云服务器中云磁盘如何挂载