Question

710. Random Pick with Blacklist

Solution

题目大意:给一个N,表示一个范围[0,N),给一个黑名单列表blacklist,其中blacklist中的元素在[0,N)范围内,调用pick方法的时候随机返回一个数,这个数满足

  1. 在[0,N)范围
  2. 不在blacklist内
  3. 要随机

思路:构造一个集合M,该M是 [0,N) - blacklist 的一个集合,调用pick时,返回[0,M)的一个随机数并根据这个随机数从集合M中取数即可。

Java实现:

class Solution {
int M;
Map<Integer, Integer> map;
Random r;
public Solution(int N, int[] blacklist) {
M = N - blacklist.length;
map = new HashMap<>();
r = new Random();
for (int tmp : blacklist) {
map.put(tmp, -1);
} for (int tmp : blacklist) {
if (tmp < M) {
while (map.containsKey(N-1)) {
N--;
}
map.put(tmp, --N);
}
}
} public int pick() {
// random in [0,N) not in blacklist
int p = r.nextInt(M);
if (map.containsKey(p)) return map.get(p);
return p;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(N, blacklist);
* int param_1 = obj.pick();
*/

710. Random Pick with Blacklist - LeetCode的更多相关文章

  1. 710 Random Pick with Blacklist

    1. 问题 给定一个黑名单,包含[0, N)的一些数,从[0, N)之间的非黑名单数中随机采样一个值. 2. 思路 字典映射 (1)计算黑名单数的长度,记作B,因为已经排除掉了B个元素,所以最后是从N ...

  2. [LeetCode] Random Pick with Blacklist 带黑名单的随机选取

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

  3. [Swift]LeetCode710. 黑名单中的随机数 | Random Pick with Blacklist

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

  4. [LeetCode] Random Pick with Weight 根据权重随机取点

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  5. LeetCode 528. Random Pick with Weight

    原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...

  6. 398. Random Pick Index - LeetCode

    Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...

  7. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  8. 【LeetCode】398. Random Pick Index 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...

  9. Leetcode: Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

随机推荐

  1. css创建叉和勾

    a{ display: inline-block; width: 10px;height:5px; background: red;line-height: 0;font-size:0;vertica ...

  2. 前端webpack workflow(二)——Webpack基本使用

    作者:Jogis原文链接:https://github.com/yesvods/Blog/issues/3转载请注明原文链接以及作者信息 前一篇文章介绍了webpack以及安装方法,这次将会介绍web ...

  3. Docker镜像构建之Dockerfile

    在 Docker 中构建镜像最常用的方式就是使用 Dockerfile.Dockerfile 是一个用来构建镜像的文本文件. 官方文档:https://docs.docker.com/engine/r ...

  4. python-逆序输出

    输入一行字符串,然后对其进行如下处理. 输入格式: 字符串中的元素以空格或者多个空格分隔. 输出格式: 逆序输出字符串中的所有元素.然后输出原列表.然后逆序输出原列表每个元素,中间以1个空格分隔.注意 ...

  5. tf.test.is_gpu_available() 返回结果为False解决办法

    安装完gpu版本的tensorflow,导入正常,但是tf.test.is_gpu_available()一直返回False,解决办法: 1.打开NVIDIA控制面板,查看CUDA的驱动版本,如果版本 ...

  6. java基础-多线程互斥锁

    多线程(JDK1.5的新特性互斥锁)* 1.同步 * 使用ReentrantLock类的lock()和unlock()方法进行同步* 2.通信 * 使用ReentrantLock类的newCondit ...

  7. c++对c的拓展_指针的引用

    套用引用公式:Type & ref =val; 假设:type 类型为int * 由公式得 int * & ref = val; // int * *const ref=&va ...

  8.  CPUs Intel 925X/915 Chipset (925X主板芯片组)

    这个是2004年的intel产品的设计(主板,主板芯片组,北桥,南桥),结构也比较清晰,主要想看南桥和北桥的设计. 一些英文解释 ECC是一种能够实现"错误检查和纠正"的技术D92 ...

  9. APSI - 2

    上一篇 APSI-1 其实就是对开源库README文件的一个翻译加上自己的一点点理解,因为篇幅过大,导致继续编辑有些卡顿,所以新开一篇继续. 前面介绍了APSI的大致技术.优化方法.以及举例说明了主要 ...

  10. ThingsBoard安装编译搭建环境踩坑记录

    1.首先从github拉下来项目,我们采用源码编译的方式部署 git clone https://github.com/thingsboard/thingsboard.git 2.切换分支 git c ...