Question

398. Random Pick Index

Solution

思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题

假设输入是:
int[] nums = new int[]{1, 2, 3, 3, 3};
int target = 3; 模拟:
1 != target pass
2 != target pass
3 == target pick的概率 nextInt(1)==0的概率 1,返回这个index的概念是1 - 1/3 -1/3 = 1/3
3 == target pick的概率 nextInt(2)==0的概率 1/2, 返回这个index的概率是1/2 * 2/3 = 1/3
3 == target pick的概率 nextInt(3)==0的概率 1/3, 返回这个index的概率是1/3

Java实现:

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

Reference

398. Random Pick Index - LeetCode的更多相关文章

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

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

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

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

  3. [leetcode] 398. Random Pick Index

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

  4. 398. Random Pick Index

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

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

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

  6. [LC] 398. Random Pick Index

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

  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. Leetcode: Random Pick Index

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

随机推荐

  1. (stm32学习总结)—LCD—液晶显示

    显示器简介 显示器属于计算机的 I/O 设备,即输入输出设备.它是一种将特定电子信息输出到屏幕上再反射到人眼的显示工具.常见的有 CRT 显示器.液晶显示器.LED 点阵显示器及OLED 显示器 本章 ...

  2. 决策树算法4:CHAID

    原理: 其中 n = a+b+c+d 卡方计算(例子)使用 sklearn完成 data.csv中的部分数据 #如何使用卡方检测相关度 from sklearn.feature_selection i ...

  3. 顺利通过EMC实验(11)

  4. (Math.round(num*100)/100).toFixed(2); 将输入的数字变成保留两位小数

    <input type="number" @input="onInputPrice" @blur="onPrice" data-id= ...

  5. 1kb 的 placeholder.js 增加 img 标签使用方式

    预览 官方网站示例. 项目 github 地址 使用 引入 placeholder.js 到你的前段代码中: <script src="placeholder.js"> ...

  6. POP3:基于命令行的电子邮件(EMail)在线查看和批量下载工具

    使用该工具可以在不安装outlook和foxmail等邮件客户端的情况下快速下载指定邮箱的邮件,并将下载的邮件以eml格式进行保存. 附: 查看eml格式的邮件可使用 EmlReader 工具,该工具 ...

  7. java中Object类是怎么回事,干嘛使的?举例说明!

    Object类的作用:m a r k - t o-        w i n: 在java中,因为所有的类都有共性,所以java的缔造者们把java设计成这样:所有的类都是Object类的直接或间接子 ...

  8. hibernate数据源

      Hibernate的描述文件可以是一个properties属性文件,也可以是一个xml文件.下面讲一下Hibernate.cfg.xml的配置.配置格式如下:1. 配置数据源 在Hibernate ...

  9. M1芯片使用cocoapods 报错[!] Oh no, an error occurred

    [解决方式] 命令行1(编译): sudo arch -x86_64 gem install ffi 命令行2(安装): arch -x86_64 pod install 原出处:https://gi ...

  10. JavaSE常用类之File类

    File类 只用于表示文件或目录的信息,不能对文件内容进行访问. java.io.File类∶代表文件和目录.在开发中,读取文件.生成文件.删除文件.修改文件的属性时经常会用到本类. File类不能访 ...