力扣算法-黑名单
作者:互联网
class Solution {
int size;
HashMap<Integer,Integer>map=new HashMap<>();
Random random;
public Solution(int n, int[] blacklist) {
random=new Random();
size=n-blacklist.length;
for(int bn:blacklist){
map.put(bn,1);
}
int last=n-1;
for(int bn:blacklist){
if(bn>=size){
continue;
}
while(map.containsKey(last)){
last--;
}
map.put(bn,last);
last--;
}
}
public int pick() {
int r= random.nextInt(size);
if(map.containsKey(r)){
return map.get(r);
}else{
return r;
}
}
}
标签:blacklist,map,last,int,bn,黑名单,力扣,算法,size 来源: https://www.cnblogs.com/2246781190zyc/p/16413260.html