710. Random Pick with Blacklist - LeetCode
Question
710. Random Pick with Blacklist

Solution
题目大意:给一个N,表示一个范围[0,N),给一个黑名单列表blacklist,其中blacklist中的元素在[0,N)范围内,调用pick方法的时候随机返回一个数,这个数满足
- 在[0,N)范围
- 不在blacklist内
- 要随机
思路:构造一个集合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的更多相关文章
- 710 Random Pick with Blacklist
1. 问题 给定一个黑名单,包含[0, N)的一些数,从[0, N)之间的非黑名单数中随机采样一个值. 2. 思路 字典映射 (1)计算黑名单数的长度,记作B,因为已经排除掉了B个元素,所以最后是从N ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- [Swift]LeetCode710. 黑名单中的随机数 | Random Pick with Blacklist
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- [LeetCode] Random Pick with Weight 根据权重随机取点
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- LeetCode 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...
- 398. Random Pick Index - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- Leetcode: Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- mysql学习 | LeetCode数据库简单查询练习
力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...
- Polymer API开发指南 (二)(翻译)
公开 property 当你公开一个 Polymer 元素的 property 名字时,就等于把这个 property 设置为公开API了.公开 property 会有如下的特性: 支持声明数据双向绑 ...
- jQuery Validate多实例讲解
规则 描述 required:true 必须输入的字段. remote:"check.php" 使用 ajax 方法调用 check.php 验证输入值. email:true 必 ...
- 大数据学习之路之ambari的安装
之前按照正常方式安装的hbase不能插入数据 所以今天来尝试下ambari能不能行 已经打了快照 如果不能还能恢复之前的样子
- String能变化吗?和StringBuffer的区别是什么
[新手可忽略不影响继续学习]看 过上面例子的童鞋一定会觉得很奇怪,s = s + s1.charAt(i); 马克-to-win, s不是老在变化吗?其实s = "";时,虚拟机会 ...
- 小程序tab栏可滑动,可点击居中demo
效果图: 代码: <view class="container"> <!-- tab导航栏 --> <!-- scroll-left属性可以控制滚动条 ...
- js知识梳理3:创建对象的模式探究
写在前面 注:这个系列是本人对js知识的一些梳理,其中不少内容来自书籍:Javascript高级程序设计第三版和JavaScript权威指南第六版,感谢它们的作者和译者.有发现什么问题的,欢迎留言指出 ...
- 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版本然后 ...
- 修改django配置文件settings
默认带数据库sqlite DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join ...
- git设置本地与远程分支关联
1.为了避免每次都进行验证,在git进行绑定ssh mkdir ~/.ssh ssh-keygen -t rsa -C "xxx@qq.com" 将生成的公钥粘贴到git中 2.本 ...