398. 随机数索引(哈希表预处理 Or 蓄水池抽样)

Question

398. 随机数索引

在这里插入图片描述在这里插入图片描述

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/random-pick-index/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Ideas

1、Answer( Java )

解法思路:哈希表预处理

⚡️ getOrDefault(Object key, V defaultValue)

获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值。

👍使用哈希表以 nums[i] 为键,下标集合 List 作为值进行存储。( ArrayList 同理)

Code①( HashMap 实现)

/**
 * @author Listen 1024
 * @description 398. 随机数索引(哈希表预处理 Or 蓄水池抽样)
 * @date 2022-04-25 10:50
 */
class Solution {
    Random random = new Random();
    Map<Integer, List<Integer>> map = new HashMap<>();

    public Solution(int[] nums) {
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            List<Integer> list = map.getOrDefault(nums[i], new ArrayList<Integer>());
            list.add(i);
            map.put(nums[i], list);
        }
    }

    public int pick(int target) {
        int res = 0;
        List<Integer> list = map.get(target);
        int i = random.nextInt(list.size());
        res = list.get(i);
        return res;
    }
}


/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */

Code②( ArrayList 实现)

/**
 * @author Listen 1024
 * @description 398. 随机数索引(哈希表预处理 Or 蓄水池抽样)
 * @date 2022-04-25 10:50
 */
 class Solution {
    ArrayList<Integer> list = new ArrayList<>();

    public Solution(int[] nums) {
        for (int num : nums) {
            list.add(num);
        }
    }

    public int pick(int target) {
        int res = 0;
        ArrayList<Integer> cnt = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == target) {
                cnt.add(i);
            }
        }
        Random random = new Random();
        int i = random.nextInt(cnt.size());
        res = cnt.get(i);
        return res;
    }

}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */

2、Answer( Java )

解法思路:蓄水池抽样

👍规定当遇到第 k 个满足 nums[i]=target 的下标时,执行一次 [0, k) 的随机操作,当随机结果为 0 时( 发生概率为 1/k ),我们将该坐标作为最新的答案候选。

Code( 蓄水池抽样 )

/**
 * @author Listen 1024
 * @description 398. 随机数索引(哈希表预处理 Or 蓄水池抽样)
 * @date 2022-04-25 10:50
 */
 class Solution {
    Random random = new Random();
    int[] _nums;

    public Solution(int[] nums) {
        _nums = nums;
    }

    public int pick(int target) {
        int index = 0, cnt = 0;
        int len = _nums.length;
        for (int i = 0; i < len; i++) {
            if (_nums[i] == target) {
                cnt++;
                if (random.nextInt(cnt) == 0) {
                    index = i;
                }
            }

        }
        return index;
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */
题解二参考链接
https://leetcode-cn.com/problems/random-pick-index/solution/by-ac_oier-zhml/
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值