398. 随机数索引

时间:2020-04-28 19:07:50   收藏:0   阅读:53
 1 class Solution 
 2 {
 3     unordered_map<int,vector<int>> hash;// 对应的数 + /下标(可能有重复数)/
 4 public:
 5     Solution(vector<int>& nums) 
 6     {
 7         for(int i = 0;i < nums.size();i ++) hash[nums[i]].push_back(i);
 8     }
 9     
10     int pick(int target) 
11     {
12         int index = rand() % hash[target].size();
13         return hash[target][index];
14     }
15 };

 

原文:https://www.cnblogs.com/yuhong1103/p/12796332.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!