Skip to content

Instantly share code, notes, and snippets.

@eclipselu
Created September 13, 2016 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eclipselu/0bdc1945937e4ddd663c02e8424a639d to your computer and use it in GitHub Desktop.
Save eclipselu/0bdc1945937e4ddd663c02e8424a639d to your computer and use it in GitHub Desktop.
Random Pick Index
class Solution {
private:
vector<int> nums;
public:
Solution(vector<int> nums) {
this->nums = nums;
srand(time(NULL));
}
int pick(int target) {
vector<int> indices;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == target)
indices.push_back(i);
}
return indices[rand() % indices.size()];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment