Java实现 LeetCode 710 黑名单中的随机数(黑白名单)
710. 黑名单中的随机数
给定一个包含 [0,n ) 中独特的整数的黑名单 B,写一个函数从 [ 0,n ) 中返回一个不在 B 中的随机整数。
对它进行优化使其尽量少调用系统方法 Math.random() 。
提示:
1 <= N <= 1000000000
0 <= B.length < min(100000, N)
[0, N) 不包含 N,详细参见 interval notation 。
示例 1:
输入:
["Solution","pick","pick","pick"]
[[1,[]],[],[],[]]
输出: [null,0,0,0]
示例 2:
输入:
["Solution","pick","pick","pick"]
[[2,[]],[],[],[]]
输出: [null,1,1,1]
示例 3:
输入:
["Solution","pick","pick","pick"]
[[3,[1]],[],[],[]]
Output: [null,0,0,2]
示例 4:
输入:
["Solution","pick","pick","pick"]
[[4,[2]],[],[],[]]
输出: [null,1,3,1]
输入语法说明:
输入是两个列表:调用成员函数名和调用的参数。Solution的构造函数有两个参数,N 和黑名单 B。pick 没有参数,输入参数是一个列表,即使参数为空,也会输入一个 [] 空列表。
class Solution {
int n=0, cnt=0;
HashSet<Integer> h=null;
boolean [] set=null;
int [] candidates=null;
public Solution(int N, int[] blacklist) {
n=N;
int L=blacklist.length, i=0;
if( N>20000)
{
h=new HashSet<Integer>();
while( i<L )
{
h.add(blacklist[i]);
i++;
}
return;
}
set=new boolean [N];
while( i<L )
{
set[blacklist[i]]=true;
i++;
}
i=0;
cnt=N-L;
candidates=new int [cnt];
int j=0;
while( i<N )
{
if( set[i]==false )
{
candidates[j]=i;
j++;
}
i++;
}
}
public int pick() {
if( n>20000 )
{
int oup=(int)(Math.random()*n);
while( h.contains(oup) )
oup=(int)(Math.random()*n);
return oup;
}
return candidates[(int)(Math.random()*cnt)];
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(N, blacklist);
* int param_1 = obj.pick();
*/
Java实现 LeetCode 710 黑名单中的随机数(黑白名单)的更多相关文章
- [Swift]LeetCode710. 黑名单中的随机数 | Random Pick with Blacklist
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- linux centos7搭建redis-5.0.5
1. 下载redis 1.1 下载地址 http://download.redis.io/releases/ 1.2 安装版本 redis-5.0.5.tar.gz 2. 安装redis 2.1 前置 ...
- html5 canvas画云
使用函数画出天空的云层图像: y 主要使用到的是数学的圆与弧度之间转换关系: 代码如下 //div对象 var parentContainer = document.getElementById(&q ...
- 不吹牛X,我真的干掉了if-else
我们在web开发中,经常使用数据库表中的字段作为"标记"来表示多个"状态",比如: 我们就以某宝的在线购物流程为例进行分析.在订单表中,使用zt字段来表示定单的 ...
- js实现图片幻灯片效果
其效果是点击图片切换到下一张图片 首先准备五张图片 <ul class="imge"> <li><img src="images/1.jpg ...
- vue 3.0新特性
参考: https://www.cnblogs.com/Highdoudou/p/9993870.html https://www.cnblogs.com/ljx20180807/p/9987822 ...
- React:Conditional Rendering(条件渲染)
就像JS中常常会根据条件(比如if/else.switch)返回不同的值,React中也可以根据组件的状态或其他参考条件返回不同的React Element. 比如根据用户是否登陆渲染对应的UI面板. ...
- jenkins+gitee+ssh自动化部署
一.准备环境 1,配置maven(MAVEN_HOME) 2,配置jdk(JAVA_HOME)我这里用的jdk1.8.0_121,之前碰到过一次别的版本的jdk在启动tomcat无法解析https情况 ...
- zz MySQL redo log及recover过程浅析
原作地址:http://www.cnblogs.com/liuhao/p/3714012.html 写在前面:作者水平有限,欢迎不吝赐教,一切以最新源码为准. InnoDB redo log 首先介绍 ...
- Lodash工具库中cloneDeep深拷贝的使用
在vue向数据库提交数据的过程中,可能需要处理某些数据,比如有以下要求:传递的参数必须是以逗号分隔的分类列表 但此时如果vue组件中的数据却是数组形式的这个时候需要用到数组的 join 方法对数据进行 ...
- GO 使用Webhook 实现github 自动化部署
通常大家开发大部分是本地git push 提交,服务器上git pull 手动更新.git 可以使用webhook实现自动部署.webhook是仓库平台的一个钩子事件,通过hook 钩子监听代码,回调 ...