[LC] 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);
class Solution {
private int[] arr;
private Random rand;
public Solution(int[] nums) {
this.arr = nums;
this.rand = new Random();
}
public int pick(int target) {
int count = 0;
int res = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != target) {
continue;
}
count += 1;
int randNum = rand.nextInt(count);
if (randNum == 0) {
res = i;
}
}
return res;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
[LC] 398. Random Pick Index的更多相关文章
- 398. Random Pick Index - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- 398. Random Pick Index随机pick函数
[抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...
- [LeetCode] 398. Random Pick Index ☆☆☆
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- [leetcode] 398. Random Pick Index
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
- 398. Random Pick Index
随机返还target值的坐标(如果存在多个target). 不太明白为什么这个题是M难度的. 无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了. 要么开始简单点,都存 ...
- 398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- 题解 P1403 【[AHOI2005]约数研究】
题目 看到题解区很多人直接给出结论:答案为 \(\displaystyle \sum_{i=1}^n\lfloor{n\over i}\rfloor\) ,没给出证明,这里给出证明 [分析] 首先,我 ...
- .net core excel导入导出
做的上一个项目用的是vs2013,传统的 Mvc模式开发的,excel报表的导入导出都是那几段代码,已经习惯了. 导入:string filename = ExcelFileUpload.FileNa ...
- 给adobe acrobat reader 添加图片注释
首先展示一下 我添加注释的结果, 下面是我的做法: 在Adobe Acrobat 中打开Sample.pdf文档,点开文档右边的“工具”-“内容”-选择“编辑对象”,鼠标光标变成实心箭头+右下角小方 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:HTML DOM 集合(Collection)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- protobuf 的enum与string转换
c/c++ enum 介绍 说起c/c++ 的enum,比起python 真的是方便简洁 enum type{ type1 = 0, type2 } enum的元素对应的int 默认从0 开始依次增加 ...
- css中标签总结
cursor CSS属性定义鼠标指针悬浮在元素上方显示的鼠标光标cursor:pointer: 小手 cursor:wait:等待....很多种 <span contenteditable=&q ...
- RaspBerry--解决无法用 ssh 直接以 root 用户登录
参考:https://www.cnblogs.com/xwdreamer/p/6604593.html 以普通用户登录,然后切换至 root 用户. 编辑 /etc/ssh/sshd_config 添 ...
- 有几个水洼(DFS)
#include <iostream> #include<cstdio> using namespace std; #define maxn 105 char field[ma ...
- macOS更新失败解决
下载离线更新包解决. https://support.apple.com/downloads/combo