398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引。 您可以假设给定的数字一定存在于数组中。
注意:
数组大小可能非常大。 使用太多额外空间的解决方案将不会通过测试。
示例:
int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);
// pick(3) 应该返回索引 2,3 或者 4。每个索引的返回概率应该相等。
solution.pick(3);
// pick(1) 应该返回 0。因为只有nums[0]等于1。
solution.pick(1);
详见:https://leetcode.com/problems/random-pick-index/description/
C++:
class Solution {
public:
Solution(vector<int> nums): v(nums) {}
int pick(int target)
{
int cnt = 0, res = -1;
for (int i = 0; i < v.size(); ++i)
{
if (v[i] != target)
{
continue;
}
++cnt;
if (rand() % cnt == 0)
{
res = i;
}
}
return res;
}
private:
vector<int> v;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
参考:https://www.cnblogs.com/grandyang/p/5875509.html
398 Random Pick Index 随机数索引的更多相关文章
- 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/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- [LeetCode] 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随机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 ...
- [leetcode] 398. Random Pick Index
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
- 398. Random Pick Index
随机返还target值的坐标(如果存在多个target). 不太明白为什么这个题是M难度的. 无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了. 要么开始简单点,都存 ...
- [Swift]LeetCode398. 随机数索引 | Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- hiho一下 第五十周 (求欧拉路径)
http://hihocoder.com/contest/hiho50/problem/1 这题有重边,所以邻接矩阵用来统计节点u,v之间有多少条边相连,并且用另外一个数组统计每个节点的入度. 然后查 ...
- Canon iP2780/iP2788 清零软件
http://www.drvsky.com/driver/iP2780_Tools.htm http://www.dyjqd.com/soft/6085.html#download http://v. ...
- html实现类似excel的复杂表格,及导出到excel
1. excellentexport.js https://github.com/jmaister/excellentexport/tree/2.0.3 2.fiddle example https ...
- 使用11g DNFS建立基于DNFS的tablespace
使用11g DNFS建立基于DNFS的tablespace 參考自: Step by Step - Configure Direct NFS Client (DNFS) on Linux (11g) ...
- ORA-00942:表或视图不存在 低级错误一例
ORA-00942:表或视图不存在 低级错误一例 运行查询语句,报ORA-00942错误 检查后发现没有指定表的所属用户.加入用户.再次查询,查询正常,截图例如以下: *************** ...
- 【POJ 3122】 Pie (二分+贪心)
id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...
- IOS View编程指南笔记
我们所示程序 对于一切IOS APP来说.我们看的的内容,都是UIView所呈现的. UIView如场景,UIWindow如舞台.UIView粉墨登场在UIWindow这个舞台上,使我们看到丰富多彩的 ...
- scikit-learn:3.3. Model evaluation: quantifying the quality of predictions
參考:http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 三种方法评估模型的预測质量: Est ...
- 基于OAS设计可扩展OpenAPI
前言 随着互联网行业的兴起,开发模式已逐步转换为微服务自治:小团队开发微服务,然后通过Restful接口相互调用.开发者们越来越渴望能够使用一种“官话”进行流畅的沟通,甚至实现多种编程语言系统的自动化 ...
- IEDA-maven引用本地jia包
简单说下为啥用maven引用本地jar包:当在pom文件中配置需要引用了jar的坐标,但是maven引用不了(原因未知情况下),这种情况下就需要找开发提供相关依赖的的jar文件打成一个jar包发送过来 ...