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);

解法:

  由于限制了空间,只能选择省空间的随机方法——水塘采样了。我们定义两个变量,计数器count和返回结果result,我们遍历整个数组,如果数组的值不等于target,直接跳过;如果等于target,count加1,然后我们在[0,count)范围内随机生成一个数字,如果这个数字是0(概率为1/count,满足条件),我们将result赋值为i即可,参见代码如下:

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

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

  1. [leetcode] 398. Random Pick Index

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

  2. 398. Random Pick Index - LeetCode

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

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

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

  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 528. Random Pick with Weight

    原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...

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

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

随机推荐

  1. 猫咪记单词——NABCD模型分析

    N ——Need 需求:学习英语是一件非常重要的事.面对各种各样的考试,学习英语,最重要的就是词汇量,背单词是提高词汇量的最直接的方法,但是单纯的背单词太单调.寻找一些合适的,更易于接受的背单词学习英 ...

  2. 【CS231N】3、Softmax分类器

    wiki百科:softmax函数的本质就是将一个K维的任意实数向量压缩(映射)成另一个K维的实数向量,其中向量中的每个元素取值都介于(0,1)之间. 一.疑问 二.知识点 1. softmax函数公式 ...

  3. EditorUtility类的说明

    SetDirty这个函数告诉引擎,相关对象所属的Prefab已经发生更改. IsPersistent用于判定对象是否是被保存在硬盘中 DisplayDialog显示一个对话框,有OK,Cancel按钮 ...

  4. mongodb授权认证 介绍

    mongodb存储所有的用户信息在admin 数据库的集合system.users中,保存用户名.密码和数据库信息.mongodb默认不启用授权认证,只要能连接到该服务器,就可连接到mongod.若要 ...

  5. 全选练习-原生版和jQuery

    今天来做一些练习,做全选练习 原生版的实现: <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  6. [2017BUAA软工]第2次个人作业

    软工第2次个人作业--代码复审 一.代码复审Check List 1.概要部分 代码能符合需求和规格说明么? 能正确处理题目要求,代码能符合需求和规格. 代码设计是否有周全的考虑? 能正确生成和解出数 ...

  7. [转帖]AMD、英特尔为何争相走向胶水多核处理器?真相在此

    AMD.英特尔为何争相走向胶水多核处理器?真相在此 胶水多核到底好不好?这个事不是简单一句话能说明的,今天的超能课堂里我们就来聊聊MCM胶水多核技术的过去及未来. 作者:孟宪瑞来源:超能网|2018- ...

  8. isset与empty 的区别

    isset()与empty()函数的区别,isset()只需要验证一个值是否存在: 而empty()不但需验证这个值是否存在,还需检验它的值是否非空和非0: 注:isset()只检验一个变量是否已经设 ...

  9. 理解 Delphi 的类(十一) - 深入类中的方法[8] - 抽象方法与抽象类

    //抽象方法类似与接口; 在没有接口的年代 Delphi 是用抽象方法来模拟接口的; 我想它最终会被接口替代. {下面就定义了两个抽象方法} TMyClass = class(TObject)   p ...

  10. Linux禁止root账户远程登录

    Linux系统中,root用户几乎拥有所有的权限,远高于Windows系统中的administrator用户权限.一旦root用户信息被泄露,对于我们的服务器来说将是极为致命的威胁.所以禁止root用 ...