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. carsim2016 与 MATLAB2018 联合仿真send to simulink后编译不成功解决方法

    之前使用CarSim8.1和Matlab17b联合仿真时遇到的问题和现在换用Carsim2017之后遇到了不一样的问题.carsim2017界面选择send to simulink 按钮之后,点击运行 ...

  2. 关于Echarts的填坑之旅

    正如标题所说,这是Echarts的一遍填坑,如果你是一些echart的配置的话可以阅读http://echarts.baidu.com/opti...的官网配置信息.今天我想给大家分享的是一些我前段时 ...

  3. 每日学习--Kociemba魔方算法

    由图可知19步还原魔方

  4. Visual Studio 2022 git error Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa

    前言 前两天因为升级了Git导致git提交拉取的时候都提示下面这个异常,然后经过一番折腾以后终于把这个问题解决了.但是今天我升级了下Visual Studio 2022将其升级到了17.1.3版本然后 ...

  5. HTTP长连接和短连接及应用情景

    HTTP短连接 HTTP/1.0中默认使用短连接, 客户端和服务器进行一次HTTP操作, 就需要建立一次连接, 任务结束连接也关闭. 当客户端浏览器访问的web网页中包含其他的web资源时, 每遇到一 ...

  6. Spring-注入方式(基于xml方式)

    1.基于xml方式创建对象 <!--配置User类对象的创建 --> <bean id="user" class="com.at.spring5.Use ...

  7. Figma禁封中国企业,下一个会是Postman吗?国产软件势在必行!

    ​ "新冷战"蔓延到生产力工具 著名 UI 设计软件 Figma 宣布制裁大疆! 近日,网上流传一份 Figma 发送给大疆的内部邮件.其中写道: "我们了解到,大疆在美 ...

  8. 微信小程序 因文件大小不能使用本地背景图片解决方法

    因微信文件只允许2m,所以不能给图片太多空间.所以出现背景图片的坑 解决方案1: 把背景图片放到服务器文件件下,直接将路径给url. 得是https开头的路径才可以 解决方案2:将图片转换成base6 ...

  9. Android第1-2周作业

    作业1:安装环境,截图编程界面,截图运行界面 作业2:九宫格 <?xml version="1.0" encoding="utf-8"?> < ...

  10. GitHub 自动合并 pr 的机器人——auto-merge-bot

    本文首发于 Nebula Graph Community 公众号 背景 作为一款开源的分布式图数据库产品,Nebula 所有的研发流程都在 GitHub 上运作.基于 GitHub 生态 Nebula ...