Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge.

Example:

int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums); // pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3); // pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);
class Solution {

    private int[] arr;
private Random rand; public Solution(int[] nums) {
this.arr = nums;
this.rand = new Random();
} public int pick(int target) {
int count = 0;
int res = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != target) {
continue;
}
count += 1;
int randNum = rand.nextInt(count);
if (randNum == 0) {
res = i;
}
}
return res;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/

[LC] 398. Random Pick Index的更多相关文章

  1. 398. Random Pick Index - LeetCode

    Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...

  2. 398. Random Pick Index随机pick函数

    [抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...

  3. [LeetCode] 398. Random Pick Index ☆☆☆

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  4. 【LeetCode】398. Random Pick Index 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...

  5. [leetcode] 398. Random Pick Index

    我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...

  6. 398. Random Pick Index

    随机返还target值的坐标(如果存在多个target). 不太明白为什么这个题是M难度的. 无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了. 要么开始简单点,都存 ...

  7. 398 Random Pick Index 随机数索引

    给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...

  8. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  9. Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

随机推荐

  1. 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL

    在实际的项目中,Aras内部的方法可能并不能完全满足我们,比如Office的组件,就必须引入,那么在Aras内部的Method中,我们如何引入外部Dll文件 首先,我们新建一个Dll文件,简单的Dem ...

  2. RewriteEngine On

    RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond ...

  3. rocketmq 使用

    rocketmq  基本使用可以看官网和官网给的demo. https://github.com/apache/rocketmq/tree/master/example 这里主要说明几个点:rocke ...

  4. tensorflow 分布式训练

    TF实现分布式流程 1.创建集群 ClusterSpec & Server cluster = tf.train.ClusterSpec({"ps": ps_hosts, ...

  5. Eclipse打开,出现Initializing Java Tooling “has encountered a problem错误,而且鼠标悬停在没有导包的类上面不会出现import信息。

    问题1:打开eclipse,出现了Initializing Java Tooling “has encountered a problem,点开详细信息,报的是空指针异常. 问题2:鼠标悬停在没有导包 ...

  6. 计算机网络(6): http cookie

    Cookie作用: 1)帮助管理用户会话信息(用户需要记录的信息:登陆状态等) 2)跟踪浏览器的行为 3)用户自定义设置 实现方式: 当用户浏览带有Cookie的网站时,网站自动为其生成一个唯一的标志 ...

  7. Python登录TP-Link路由器换ip脚本

    有些时候我们需要更换IP(你懂得),网络下载的拨号软件大部分是需要电脑直接链接调制解调器(猫),对于局域网用户来说就比较麻烦了,下面我们用python来实现登录路由器自动切换ip的功能 # -*- c ...

  8. emacs 配置文件

    使用方式: git clone 到本地,把 emacs 复制到 ~/.emacs.d 打开 emacs 会自动安装包 https://github.com/NorseLZJ/lzj-config/tr ...

  9. Reservoir Computing论文学习

    目录 背景: RC优势: 储备池计算主要理论组成: ESNS数学模型 结构表示 状态方程和输出方程 计算过程 储备池的优化 GA:使用进化算法对参数进行优化: 基于随机梯度下降法的储备池参数优化 参考 ...

  10. VMware12 + Ubuntu16.04 虚拟磁盘扩容

    转载自:https://blog.csdn.net/Timsley/article/details/50742755 今天用虚拟机的时候,发现虚拟机快满了,提示磁盘空间小,不得不扩充虚拟机空间.经过百 ...