首页 > 其他 > 详细

398. Random Pick Index

时间:2018-05-06 15:49:50      阅读:183      评论:0      收藏:0      [点我收藏+]

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++) {
            if (m[i] == target) {
                cnt++;
                if (rand() % cnt == 0)
                    res = i;
            }
        }
        return res;
    }
};

 

398. Random Pick Index

原文:https://www.cnblogs.com/JTechRoad/p/8998254.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!