给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引。 您可以假设给定的数字一定存在于数组中。
注意:
数组大小可能非常大。 使用太多额外空间的解决方案将不会通过测试。
示例:
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 随机数索引的更多相关文章

  1. 398. Random Pick Index - LeetCode

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

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

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

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

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

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

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

  5. [LC] 398. Random Pick Index

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

  6. [leetcode] 398. Random Pick Index

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

  7. 398. Random Pick Index

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

  8. [Swift]LeetCode398. 随机数索引 | Random Pick Index

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

  9. [LeetCode] Random Pick Index 随机拾取序列

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

随机推荐

  1. 在windows下安装Django

    2013-07-24 21:23:30| 1.安装Python 我安装的是Python(x,y)-2.7.5.0,安装目录在C盘.安装成功后如下图所示.   2.安装Django 从https://w ...

  2. 18.10.7 POIN 模拟赛

    期望 :80+ +90+40=210+ 实际 :30+90+0=120 链接:https://www.nowcoder.com/acm/contest/175/A来源:牛客网 时间限制:C/C++ 1 ...

  3. operamasks—omGrid/omBorderLayout的混合使用

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs&q ...

  4. 有用的 SystemTap 脚本

    https://segmentfault.com/a/1190000000680628 https://github.com/posulliv/stap

  5. SqlServer函数获取指定日期后的第某个工作日

    获取工作日 需要编写一个SqlServer函数,F_getWorkday,传入两个参数,第一个为时间date,第二个参数为第几个工作日num.调用F_getWorkday后返回date之后的第num个 ...

  6. 纤程(FIBER)

    Indy 10 还包含对纤程的支持.纤程是什么?简单来说,它也是 一个“线程”,但是它是由代码控制的,而不是由操作系统控制的.实际上,可以认为线程 是一个高级纤程.纤程和 Unix 用户线程(Unix ...

  7. POJ3977 Subset 折半枚举

    题目大意是给定N个数的集合,从这个集合中找到一个非空子集,使得该子集元素和的绝对值最小.假设有多个答案,输出元素个数最少的那个. N最多为35,假设直接枚举显然是不行的. 可是假设我们将这些数分成两半 ...

  8. ArcGIS 教程:Workflow Manager 高速浏览

    应用程序概述 Workflow Manager 用户界面提供了用于在整个作业的生命周期中创建和管理作业的工具. 下面全部信息将会在本帮助文档的兴许章节中进行具体的说明. 文件菜单 新建 - 在系统中创 ...

  9. CodeIgniter 向mysql插入数据包括字母、汉字问题

    今天在使用ci框架,须要向mysql数据表插入数据.当中的一个字段包括汉字.字母.但是用传统的使用sql语句:insert into XXX这样的方式,不管怎样都插入不成功,最后我换了还有一种方式: ...

  10. 改动ScrollView的滑动速度和解决ScrollView与ViewPager的冲突

    话不多说,非常easy,能够从凝视中知道做法,直接上代码: 1.改动ScrollView的滑动速度: public class MyHorizontalScrollView extends Horiz ...