解题思路:
实现[0,n-m]区间黑名单对[n-m, n]的非黑名单映射,然后取[0,n-m]随机数,如果是白名单直接返回,否则返回映射的白名单数,代码如下:
class Solution {
unordered_map b2w;
int bound;
public:
Solution(int n, vector &blacklist) {
int m = blacklist.size();
bound = n - m;
unordered_set 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;
}
};


![LeetCode 710 黑名单中的随机数[随机数] HERODING的LeetCode之路 LeetCode 710 黑名单中的随机数[随机数] HERODING的LeetCode之路](http://www.mshxw.com/aiimages/31/995970.png)
