[LeetCode] 398. Random Pick Index ☆☆☆
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 ☆☆☆的更多相关文章
- [leetcode] 398. Random Pick Index
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
- 398. Random Pick Index - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- 398. Random Pick Index
随机返还target值的坐标(如果存在多个target). 不太明白为什么这个题是M难度的. 无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了. 要么开始简单点,都存 ...
- 398. Random Pick Index随机pick函数
[抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...
- [LC] 398. Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...
- LeetCode 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- vs2013+python+ cocos2d-x-3.3rc0环境搭建
1.vs2013安装一路next,安装即可,时间1~2个小时 2.解压cocos2d-x-3.3rc0 build文件夹里会有名为 cocos2d-win32.vc2012的sln文件 打开 ...
- c++课的圆周面积
又回顾了一下一两个月没动过的类,似乎又有点手生了,不过还好还可以做. 在栋哥的推荐下下载了一个vs2015,表示从dev的白鼠形式的简单操作缓过来还有些不习惯呢,不过有些功能,例如诊断还是挺好用的 这 ...
- 结对作业(1.0版)(bug1已修复)
import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing ...
- 09_Java面向对象_第9天(类、封装)_讲义
今日内容介绍 1.面向对象思想 2.类与对象的关系 3.局部变量和成员变量的关系 4.封装思想 5.private,this关键字 6.随机点名器 01面向对象和面向过程的思想 A: 面向过程与面向对 ...
- 第三章 深入Servlet技术
3.1 配置Servlet <servlet-name>,<servlet-class>是必须配置的,以便于web容器知道浏览器具体访问的是哪个servlet. <ini ...
- wait 和 sleep 区别
/* wait 和 sleep 区别? 1,wait可以指定时间也可以不指定. sleep必须指定时间. 2,在同步中时,对cpu的执行权和锁的处理不同. wait:释放执行权,释放锁. sleep: ...
- object & over-write
object & over-write
- 在sql server ide里数据修改数据
在sql server 的客户端工具ssms里,只有在工具里打开后直接修改. 除了用这种方法外,还有其它方法可以改吗?比如像pl/sql里的for update sql server的客户端功能比较差 ...
- 【转载】css3动画简介以及动画库animate.css的使用
原文地址:http://www.cnblogs.com/2050/p/3409129.html 在这个年代,你要是不懂一点点css3的知识,你都不好意思说你是个美工.美你妹啊,请叫我前端工程师好不好. ...
- MT【148】凸数列
(2018浙江省赛13题) 设实数$x_1,x_2,\cdots,x_{2018}$满足$x_{n+1}^2\le x_nx_{n+2},(n=1,2,\cdots,2016)$和$\prod\lim ...