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

原文地址:https://www.cnblogs.com/xidian2014/p/8854462.html

时间: 2024-08-17 05:20:22

398 Random Pick Index 随机数索引的相关文章

[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 spa

398. Random Pick Index

https://leetcode.com/problems/random-pick-index/description/ Reservoir Sampling class Solution { public: vector<int> m; Solution(vector<int> nums) { m = nums; } int pick(int target) { int res = -1; for (int i = 0, cnt = 0; i < m.size(); i++

leetcode_398 Random Pick Index(Reservoir Sampling)

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 spa

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 sp

Leetcode: 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 sp

ORACLE Index Lookup索引访问路径总结

在ORACLE中,索引访问/查找(Index Lookup)路径有五种方式,分别为INDEX UNIQUE SCAN.INDEX RANGE SCAN.INDEX FULL SCAN.INDEX FAST FULL SCAN .INDEX SKIP SCAN.下面通过一些案例介绍.总结一下这五种索引访问路径.本文是总结这方面的知识点,所以文中一些地方参考.引用了参考资料中的部分内容.详细.具体资料可以参考官方资料Index Scans 索引唯一扫描(INDEX UNIQUE SCAN)   索引

mysql force index() 强制索引的使用

mysql force index() 强制索引的使用 之前跑了一个SQL,由于其中一个表的数据量比较大,而在条件中有破坏索引或使用了很多其他索引,就会使得sql跑的非常慢... 那我们怎么解决呢? 这时候我么可以使用mysql force index() 强制索引来优化查询语句: 使用MySQL force index 强制索引的目的是对目标表添加最关键的索引,使其优先使用该索引筛选数据: select * from ws_shop a where date(create_time-inter

MySQL force Index 强制索引概述

以下的文章主要介绍的是MySQL force Index  强制索引,以及其他的强制操作,其优先操作的具体操作步骤如下:我们以MySQL中常用的hint来进行详细的解析,如果你是经常使用Oracle的朋友可能知道,Oracle的hincvt功能种类很多,对于优化sql语句提供了很多方法. 同样,在MySQL里,也有类似的hint功能.下面介绍一些常用的. 强制索引MySQL FORCE INDEX SELECT * FROM TABLE1 FORCE INDEX (FIELD1) … 以上的SQ

基础:从概念理解Lucene的Index(索引)文档模型

转:http://blog.csdn.net/duck_genuine/article/details/6053430 目录(?)[+] Lucene主要有两种文档模型:Document和Field,一个Document可能包含若干个Field. 每一个Field有不同的策略: 1.被索引 or not,将该字段(Field)经过分析(Analyisi)后,加入索引中,并不是原文 . 2.如果被索引,可选择是否保存“term vector”(向量),用于相似检索. 3.可选择是否存储(store