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);
 
这道题指明了我们不能用太多的空间,那么省空间的随机方法只有水塘抽样Reservoir Sampling了,LeetCode之前有过两道需要用这种方法的题目Shuffle an ArrayLinked List Random Node。那么如果了解了水塘抽样,这道题就不算一道难题了,我们定义两个变量,计数器cnt和返回结果res,我们遍历整个数组,如果数组的值不等于target,直接跳过;如果等于target,计数器加1,然后我们在[0,cnt)范围内随机生成一个数字,如果这个数字是0,我们将res赋值为i即可,参见代码如下:
class Solution {
public:
Solution(vector<int> nums): v(nums) {} int pick(int target) {
int cnt = , res = -;
for (int i = ; i < v.size(); ++i) {
if (v[i] != target) continue;
++cnt;
if (rand() % cnt == ) res = i;
}
return res;
}
private:
vector<int> v;
};

类似题目:

Shuffle an Array

Linked List Random Node

参考资料:

https://discuss.leetcode.com/topic/58371/c-o-n-solution/2

https://discuss.leetcode.com/topic/58297/share-c-o-n-time-solution/2

https://discuss.leetcode.com/topic/58403/bruce-force-java-with-o-n-time-o-1-space

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Random Pick Index 随机拾取序列的更多相关文章

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

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

  2. Leetcode: Random Pick Index

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

  3. [LeetCode] Random Pick with Blacklist 带黑名单的随机选取

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

  4. [LeetCode] Random Pick with Weight 根据权重随机取点

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  5. 398. Random Pick Index - LeetCode

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

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

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

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

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

  8. [leetcode] 398. Random Pick Index

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

  9. 398. Random Pick Index

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

随机推荐

  1. 【分布式】Zookeeper会话

    一.前言 前面分析了Zookeeper客户端的细节,接着继续学习Zookeeper中的一个非常重要的概念:会话. 二.会话 客户端与服务端之间任何交互操作都与会话息息相关,如临时节点的生命周期.客户端 ...

  2. iOS: 在iPhone和Apple Watch之间共享数据: App Groups

    我们可以在iPhone和Apple Watch间通过app groups来共享数据.方法如下: 首先要在dev center添加一个新的 app group: 接下来创建一个新的single view ...

  3. eclipse中Maven运行时报错: -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.

    1.安装 Maven 如果需要使用到 Maven ,必须首先安装 Maven , Maven 的下载地址在 Apache Maven 中有,您也可以点击这里下载 zip ,tar.gz. 下载好 Ma ...

  4. Attribute操作的性能优化方式

    Attribute是.NET平台上提供的一种元编程能力,可以通过标记的方式来修饰各种成员.无论是组件设计,语言之间互通,还是最普通的框架使 用,现在已经都离不开Attribute了.迫于Attribu ...

  5. pandas.DataFrame排除特定行

    使用Python进行数据分析时,经常要使用到的一个数据结构就是pandas的DataFrame 如果我们想要像Excel的筛选那样,只要其中的一行或某几行,可以使用isin()方法,将需要的行的值以列 ...

  6. 来玩Play框架06 用户验证

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 用户验证(User Authentification)复合的使用Play框架的数个 ...

  7. 手游聚合SDK开发之远程开关---渠道登入白名单

    白名单有啥好说的呢?无非就是筛选登入,大家第一眼看到就是这个印象,白名单也是有文章的,弄的时机不同会给你带来很不错的收益,注意是收益.还是举例来说,游戏上线前渠道都会做一个预下载,一般提前1-2天,这 ...

  8. mysql awr 1.0.5 GA正式版发布

    1.0.5变更内容 1.修复centos 7下swap值不正确:2.中文乱码:3.begin/end snap下拉显示Mysql启动时间:4.两次快照间不能重启过:5.新增tab页面查看mysql存储 ...

  9. JS阻止事件冒泡

    在使用JS事件的时候,外层元素事件有可能被里层元素的事件触发,例如点击里层元素外层也触发了点击,这种现象称为事件冒泡.(李昌辉) <div id="wai"> < ...

  10. MS SQL按IN()内容排序

    需求:MMSQL查询结果,按查询条件中关键字IN内的列举信息的顺序一一对应排序. 分析:使用CHARINDEX 函数. 解决方法: SELECT * FROM Product WHERE 1=1 AN ...