Fisher–Yates shuffle 洗牌算法
海量数据随机抽样问题(蓄水池问题)
题目
class Solution { private: vector<int> vc; public: Solution(vector<int> nums) { vc = nums; } int pick(int target) { vector<int> tmp; int size = 0; for (int i = 0; i < vc.size(); ++i) { if (target == vc[i]) { if (tmp.size() != 0) { int lim = 66666666 - 66666666 % i; int id = rand(); while (id > lim) id = rand(); id %= ++size; if (id == 0) tmp[id] = i; } else { ++size; tmp.push_back(i); } } } return tmp[0]; } };
原文:https://www.cnblogs.com/liuweimingcprogram/p/9704319.html