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 黑名单中的随机数(黑白名单)的更多相关文章

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

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

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. SAP CSO1创建BOM

      1业务说明 此文档使用BAPI:BAPI_MATERIAL_BOM_GROUP_CREATE创建BOM 2前台实现 事务代码:CS01 输入行项目信息 保存即可 3代码实现 3.1调用BAPI 抬 ...

  2. Android 8.1 关机充电动画(二)Uboot模式

    system:Android 8.1 platform:RK3326/PX30 uboot kernel Android 8.1 关机充电动画(一)模式选择 Android 8.1 关机充电动画(二) ...

  3. 初探Redis-基础类型Hash

    Redis存在五种基础类型:字符串(String).队列(List).哈希(Hash).集合(Set).有序集合(Sorted Set).本次列举出Hash的常用操作. Redis官网:https:/ ...

  4. [csu/coj 1078]多个序列的最长公共子序列

    题意:给n个序列,同一个序列里面元素互不相同,求它们的最长公共子序列. 思路:任取一个序列,对于这个序列里面的两个数ai,aj(i<j),如果对于其它每一个序列,都出现过ai,aj,且ai在aj ...

  5. 进程和线程—Python多线程编程

    进程和线程 进程 进程是一个执行中的程序.每个进程都拥有自己的地址空间.内存.数据栈以及其它用于跟踪执行的辅助数据. 一个程序运行就是一个进程(比如 QQ.微信或者其它软件): 进程可以通过派生新的进 ...

  6. window 10电脑永不熄屏的方法

    你的电脑是不是人还没有离开一会儿,经常锁屏,输入密码??反复反复,特别的折磨人,别急,下面我教你,告别反复,从此我的电脑我做主. 第一步,打开设置,进入个性化界面,点击锁屏界面,往下滑 第二步,找到屏 ...

  7. android Viewpager禁用/开启滑动切换功能

    要实现viewpager的滑动以及禁止滑动切换功能只需要继承viewpager,在onTouchEvent进行逻辑判断即可(网上搜到的,确实可行,原创地址不明),下面自己实现一个 import and ...

  8. python datetime 转timestamp

    import datetime import time d1=datetime.date.today() t1=time.mktime(d1.timetuple()) d2=datetime.date ...

  9. Django之from.Form内置字段

    from django import forms Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label标签或显示内 ...

  10. 正则+re模块知识总结

    目录 正则表达式 定义+特点 元字符 量词 贪婪匹配与非贪婪匹配 转义符 re模块 re.findall re.search re.match re.compile re.finditer re.sp ...