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. C# CHM帮助文档

    1.生成chm文件 首先,下载EasyCHM软件,此软件可将HTML文件.TXT文件.图片和文件夹按照文件层次生成.chm文件.EasyCHM打开界面如图所示: 点击“新建”,选择需要生成.chm文件 ...

  2. 《大象Think in UML》阅读笔记之一

    Think in UML这一书以UML为载体,将面向对象的分析设计思想巧妙地融合在建模UML当中,通过一些实例将软件系统的开发过程中的一些知识有机地结合起来.全书共分为四篇:准备篇.基础篇.进阶篇和总 ...

  3. python learning2.py

    L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] # 取前3个元素的笨方法 r = [] n = 3 for i in range(n): r.appe ...

  4. 【CSAPP笔记】12. 高速缓存存储器

    高速缓存存储器 在存储层次结构中,高速缓存存储器,也叫 cache 是最接近 CPU 寄存器的那一块. 更一般而言,缓存(caching)是一个无所不在的技术.缓存的意思是:对于每层的存储设备,位于 ...

  5. mysql 性能分析及explain用法

    转载自http://blog.sina.com.cn/s/blog_4586764e0100o9s1.html 使用explain语句去查看分析结果 如   explain select * from ...

  6. sql中Union和union all的使用

    该文转载自:http://www.cnblogs.com/chaobaojun/archive/2009/12/24/1631508.html 在MS-SQL如果将两个或更多查询的结果组合为单个结果集 ...

  7. Beta阶段团队项目开发篇章3

    例会时间 2016.12.6晚 例会照片 个人工作 上阶段任务验收 中英文切换功能已经实现,调查结果分析已经完成,博客基本撰写完成,在征求其他组员意见后发布.任务基本完成. 任务分配 组员 任务内容 ...

  8. JS面向对象(封装,继承)

    在六月份找工作中,被问的最多的问题就是: js面向对象,继承,封装,原型链这些,你了解多少? 额,,,我怎么回答呢, 只能说,了解一些,不多不少,哈哈哈哈,当然,这是玩笑话. 不过之前学过java,来 ...

  9. PHP 常用函数总结(四)

    9.PHP常用判断函数 is_bool();//判断是否为布尔型 is_float(); //判断是否为浮点型 is_int(); //判断是否为整型 is_numeric(); //判断是否为数值型 ...

  10. Oracle12c 之后的路线图

    Oracle18c 以及 Oracle19c 的原始版本信息 装载一下别人的博客内容 http://www.cnblogs.com/zhjh256/p/9816499.html 感谢原作者.. 另外  ...