题目描述
给你一个可能含有 重复元素 的整数数组 nums ,请你随机输出给定的目标数字 target 的索引。你可以假设给定的数字一定存在于数组中。
实现 Solution 类:
Solution(int[] nums) 用数组 nums 初始化对象。
int pick(int target) 从 nums 中选出一个满足 nums[i] == target 的随机索引 i 。如果存在多个有效的索引,则每个索引的返回概率应当相等。
示例:
输入
["Solution", "pick", "pick", "pick"]
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
输出
[null, 4, 0, 2]
解释
Solution solution = new Solution([1, 2, 3, 3, 3]);
solution.pick(3); // 随机返回索引 2, 3 或者 4 之一。每个索引的返回概率应该相等。
solution.pick(1); // 返回 0 。因为只有 nums[0] 等于 1 。
solution.pick(3); // 随机返回索引 2, 3 或者 4 之一。每个索引的返回概率应该相等。
提示:
1 <= nums.length <= 2 * 104
-231 <= nums[i] <= 231 - 1
target 是 nums 中的一个整数
最多调用 pick 函数 104 次
解题思路
哈希表
定义一个哈希表,存储每个target对应的 索引列表,当要查询target时,根据随机数随机返回一个列表中的索引。难度中等。
class Solution {
private HashMap<Integer, ArrayList<Integer>> map;
Random random;
public Solution(int[] nums) {
map=new HashMap<>();
random=new Random();
for(int i=0;i<nums.length;i++){
if(!map.keySet().contains(nums[i]))
map.put(nums[i],new ArrayList<>());
map.get(nums[i]).add(i);
}
}
public int pick(int target) {
ArrayList<Integer> arr=map.get(target);
return arr.get(random.nextInt(arr.size()));
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
水塘抽样算法
class Solution {
int[] nums;
Random random;
public Solution(int[] nums) {
random=new Random();
this.nums=nums;
}
public int pick(int target) {
int ans=-1;
int k=1;// 要随机选k个值
for(int i=0,cnt=0;i<nums.length;i++){
if(nums[i]==target){
cnt++;
if(random.nextInt(cnt)<k) ans=i;
}
}
return ans;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/