LeetCode——398. 随机数索引

简介: LeetCode——398. 随机数索引

398. 随机数索引


题目描述

答案

我的代码

官网答案

方法一:哈希表

方法二:水塘抽样


题目描述


给你一个可能含有 重复元素 的整数数组 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 次


答案


我的代码


class Solution {
    int[] arr ;
    public Solution(int[] nums) {
        arr = nums;
    }
    public int pick(int target) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==target){
                list.add(i);
            }
        }
        // 生成随机数
        Random random = new Random();
        // 返回结果
        return list.get(random.nextInt(list.size()));
    }
}


官网答案


方法一:哈希表


如果不考虑数组的大小,我们可以在构造函数中,用一个哈希表 pos 记录 nums 中相同元素的下标。

对于 pick 操作,我们可以从 pos 中取出 target 对应的下标列表,然后随机选择其中一个下标并返回。

class Solution {
    Map<Integer, List<Integer>> pos;
    Random random;
    public Solution(int[] nums) {
        pos = new HashMap<Integer, List<Integer>>();
        random = new Random();
        for (int i = 0; i < nums.length; ++i) {
            pos.putIfAbsent(nums[i], new ArrayList<Integer>());
            pos.get(nums[i]).add(i);
        }
    }
    public int pick(int target) {
        List<Integer> indices = pos.get(target);
        return indices.get(random.nextInt(indices.size()));
    }
}


复杂度分析


时间复杂度:初始化为 O(n),pick 为 O(1),其中 n 是 nums 的长度。

空间复杂度:O(n)。我们需要 O(n) 的空间存储 n 个下标。


方法二:水塘抽样


class Solution {
    int[] nums;
    Random random;
    public Solution(int[] nums) {
        this.nums = nums;
        random = new Random();
    }
    public int pick(int target) {
        int ans = 0;
        for (int i = 0, cnt = 0; i < nums.length; ++i) {
            if (nums[i] == target) {
                ++cnt; // 第 cnt 次遇到 target
                if (random.nextInt(cnt) == 0) {
                    ans = i;
                }
            }
        }
        return ans;
    }
}

时间复杂度:初始化为 O(1),pick 为 O(n),其中 n 是 nums 的长度。

空间复杂度:O(1)。我们只需要常数的空间保存若干变量。

相关文章
|
2天前
|
索引
Leetcode 给定一个数组,给定一个数字。返回数组中可以相加得到指定数字的两个索引
Leetcode 给定一个数组,给定一个数字。返回数组中可以相加得到指定数字的两个索引
|
7月前
|
人工智能 BI 索引
【Leetcode -598.范围求和Ⅱ -599.两个列表的最小索引总和】
【Leetcode -598.范围求和Ⅱ -599.两个列表的最小索引总和】
26 0
|
9月前
|
索引
LeetCode-398 随机数索引
LeetCode-398 随机数索引
|
2天前
|
开发者 索引 Python
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
28 0
|
索引
LeetCode每日一题(9)——随机数索引(理解水塘抽样)
随机数索引 1.题目 2.示例 3.思路及代码
100 0
LeetCode每日一题(9)——随机数索引(理解水塘抽样)
|
索引 Python
LeetCode 599. 两个列表的最小索引总和
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
74 0
|
算法
LeetCode每日一题——710. 黑名单中的随机数
优化你的算法,使它最小化调用语言 内置 随机函数的次数。
74 0
|
2天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
2天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
12 0