Created
September 13, 2016 06:11
-
-
Save eclipselu/0bdc1945937e4ddd663c02e8424a639d to your computer and use it in GitHub Desktop.
Random Pick Index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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