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. web概念简述,HTML学习笔记

    今日内容 1. web概念概述 2. HTML web概念概述 * JavaWeb: * 使用Java语言开发基于互联网的项目 * 软件架构: 1. C/S: Client/Server 客户端/服务 ...

  2. 设计模式之GOF23建造者模式

    组件很多,装配顺序不定 本质: 1,分离了对象子组件的单独构造(Builder负责)和装配(Director负责),从而可以构造出复杂的对象,这个模式适用于某个对象的构建过程复杂的情况下使用 2,实现 ...

  3. JDBC02 加载JDBC驱动 建立连接

    JDBC(Java Database Connection)为Java开发者使用数据库提供了统一的编程接口 sun公司由于不知道各个主流商用数据库的程序代码,因此无法自己写代码连接各个数据库,因此su ...

  4. Mac 安装实用开发软件和日常软件清单

    软件安装 开发需要安装软件 HomeBrew 这个是 mac 的软件包管理软件,类似于 yum 安装 rpm 包会帮我们处理软件包之间的依赖关系一样,或者 apt-get 安装 deb 包,最开始接触 ...

  5. 【原理探究】女朋友问我ArrayList遍历时删除元素的正确姿势是什么?

    简介 我们在项目开发过程中,经常会有需求需要删除ArrayList中的某个元素,而使用不正确的删除方式,就有可能抛出异常.或者在面试中,会遇到面试官询问遍历时如何正常删除元素.所以在本篇文章中,我们会 ...

  6. 网页爬虫--python3.6+selenium+BeautifulSoup实现动态网页的数据抓取,适用于对抓取频率不高的情况

    说在前面: 本文主要介绍如何抓取 页面加载后需要通过JS加载的数据和图片 本文是通过python中的selenium(pyhton包) + chrome(谷歌浏览器) + chromedrive(谷歌 ...

  7. 【漫画】互斥锁ReentrantLock不好用?试试读写锁ReadWriteLock

    ReentrantLock完美实现了互斥,完美解决了并发问题.但是却意外发现它对于读多写少的场景效率实在不行.此时ReentrantReadWriteLock来救场了!一种适用于读多写少场景的锁,可以 ...

  8. An invalid domain [.test.com] was specified for this cookie 原因分析

    java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie 以上博客 ...

  9. springMVC 重定向带参数

    重定向时经常需要带上一定的标志位参数,当然,强大的springmvc提供了便利的操作. 只需要在方法参数中添加RedirectAttributes 或其子类即可! @RequestMapping(&q ...

  10. mp4封装格式各box类型讲解及IBP帧计算

    mp4封装格式各box类型讲解及IBP帧计算 目录 mp4封装格式各box类型讲解及IBP帧计算 box ftyp box moov box mvhd box (Movie Header Box) t ...