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; } };
原文地址:https://www.cnblogs.com/JTechRoad/p/8998254.html
时间: 2024-10-08 05:19:49