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. pinpoint 单机HBASE数据量过大问题解决

    Pinpoint接入业务监控后数据量大涨,平均每周Hbase数据增量35G左右,数据量太大,需要对数据进行定期清理,否则监控可用性降低. 操作步骤 查找出数据大的hbase表 [root@iZ28ov ...

  2. leetcode--200--python(深度广度优先遍历实现代码)

    点滴积累,厚积薄发,做好每一天,向时间要效率,向生命要质量. 一.深度优先搜索和广度优先搜索DFS(Depth-First-Search),是盲目搜索算法的一种.常常用在树的遍历及图的处理上.假设当前 ...

  3. linux下创建swap分区

    两种不同的方式创建swap分区 第一种方法: fdisk /dev/sda n (新建一个分区为/dev/sda6) t (修改分区的id) 82 (swap的id为82) w (重写分区表) par ...

  4. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数调用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Qt5学习笔记(1)-环境配置(win+64bit+VS2013)

    Qt5学习笔记(1)-环境配置 工欲善其事必先-不装-所以装软件 久不露面,赶紧打下酱油. 下载 地址:http://download.qt.io/ 这个小网页就可以下载到跟Qt有关的几乎所有大部分东 ...

  6. 编写shell脚本,使用 nohup 让springboot 项目在后台持续运行

    1.将springboot项目打成jar放在linux的某个目录下. 2.新建一个nohup.log文件. 3.使用vi命令新建一个start.sh文件并写下以下内容: #!/bin/sh nohup ...

  7. Spring Cloud Alibaba 教程 | Nacos(五)

    扩展配置(extended configurations) 通过之前的学习,我们知道应用引入nacos配置中心之后默认将会加载Data ID= ${prefix} - ${spring.profile ...

  8. image compression with libjpeg

    http://www.aaronmr.com/en/2010/03/test/ Working on the project I've seen in the need for compression ...

  9. 二、提高期(Upping the Ante)

    二.提高期(Upping the Ante) Upping the Ante?这可是第四阶段的词.没办法,Greg Thomson用这个词代表第二阶段,看着喜欢,继续沿用. 经过两三个月的“图象+声音 ...

  10. pandas读取和写入excel多个sheet表单

    一.读取单个表单 import pandas as pd excel_reader=pd.ExcelFile('文件.xlsx') # 指定文件 sheet_names = excel_reader. ...