原题链接:Leetcode 398. Random Pick Index

Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Implement the Solution class:

  • Solution(int[] nums) Initializes the object with the array nums.
  • int pick(int target) Picks a random index i from nums where nums[i] == target. If there are multiple valid i’s, then each index should have an equal probability of returning.

Example 1:

Input
["Solution", "pick", "pick", "pick"]
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
Output
[null, 4, 0, 2]Explanation
Solution solution = new Solution([1, 2, 3, 3, 3]);
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.

Constraints:

  • 1 <= nums.length <= 2 * 104
  • -231 <= nums[i] <= 231 - 1
  • target is an integer from nums.
  • At most 104 calls will be made to pick.

方法一:哈希表

思路:

用一个映射值和对应下标的数组的哈希表来存储信息,pick(i)实现从i对应的数组中返回随机值即可

c++代码:

class Solution {// <值, 下标数组>的映射unordered_map<int, vector<int>> pos;
public:Solution(vector<int> &nums) {for (int i = 0; i < nums.size(); ++i) {pos[nums[i]].push_back(i);}}int pick(int target) {auto &indices = pos[target];return indices[rand() % indices.size()];}
};/*** Your Solution object will be instantiated and called as such:* Solution* obj = new Solution(nums);* int param_1 = obj->pick(target);*/

复杂度分析:

  • 时间复杂度:O(n),开销在初始化
  • 空间复杂度:O(n),需要存储n个数的信息到哈希表

Leetcode 398.随机数索引相关推荐

  1. Java实现 LeetCode 398 随机数索引

    398. 随机数索引 给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中. 注意: 数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试 ...

  2. Leetcode 398. 随机数索引 解题思路及C++实现

    解题思路: 题目对空间复杂度有要求.在构造函数中,应该要先把nums数组拷贝一下. 在pick函数中,必然要遍历数组nums,这样才能得到 target 值对应的所有索引,之后再生成一个随机数,返回任 ...

  3. LeetCode 398. 随机数索引(概率)

    1. 题目 给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中. 注意: 数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试. 示例: ...

  4. LeetCode 398. 随机数索引

    1.题目 https://leetcode-cn.com/problems/random-pick-index/ 2.题解 蓄水池抽样 其思路就是循环之后先判断一下这个数在数组中的索引, 然后随机选出 ...

  5. LeetCode 398 随机数索引 Python

    给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中. 注意: 数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试. 示例: int[] ...

  6. 398. 随机数索引 ( 设计 )

    LeetCode:398. 随机数索引 要求返回 target 在数组中随机的一个下标 留意,这个数组不是有序的,有序的话使用 二分查找. 这里无序的, 直接保存相同元素的下标,然后随机返回 AC C ...

  7. leetcode刷题记录-398. 随机数索引

    前言 今天的题目为中等,跟之前碰到过的一道题思路很相似,利用map表来空间换时间,以此来做到节省时间复杂度. 每日一题 今天的题目是 398. 随机数索引,难度为中等 给你一个可能含有 重复元素 的整 ...

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

    文章目录 Question Ideas 1.Answer( Java ) `⚡️ getOrDefault(Object key, V defaultValue)` Code①( HashMap 实现 ...

  9. LeetCode系列398—随机数索引(蓄水池抽样)

    题意 给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中. 注意: 数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试. 示例: in ...

最新文章

  1. SaaS加速器II 能力中心:互利互补 共享商业红利
  2. COCO数据集数据转换为XML格式
  3. 银行、航空软件结构图
  4. React Native ios打包
  5. For the king:出色的冒险,失败的角色扮演
  6. Eclipse Validating减少不必要的验证
  7. 课堂练习 5-22 团队如何做决定
  8. 单片机测量脉宽c语言程序,51单片机hc-sr04超声波测距(脉宽测量)DEMO程序
  9. SSH连接树莓派+设置开机自启动程序
  10. spring之AOP
  11. 初学者这样玩 TypeScript,迟早进大厂系列!
  12. 三种快排及四种优化方式
  13. RTX的“远程登录”原理是什么?
  14. WindowsServer2012r2远程桌面多用户同时远程连接设置
  15. html5调用安卓锁屏,HTML5实现APP永不锁屏
  16. WSL2加载独立硬盘和设置固定IP
  17. setenv,getenv,fork
  18. C++第一天(编写第一个程序,变量与常量)
  19. C++操作word:插入文字、图片、表格,设置样式字体
  20. speedoffice(Excel)表格中怎么自动调整行高列宽?

热门文章

  1. 教授专栏18 | 黄阳光: 建网络信誉系统 增线上促销成效
  2. QtCreator画UML
  3. 2021年安全员-C证考试题及安全员-C证模拟试题
  4. linux bonding 原理,Linux bonding介绍
  5. android 自定义五边形图片,Android自定义View-蜘蛛网属性图(五边形图)
  6. 原理+代码|手把手教你使用Python实战反欺诈模型
  7. 【python实战(1),中信银行Java笔试题库
  8. 2022/07/25 吉软 Java基础(10)面向对象——多态
  9. Activiti7学习七之流程实例的挂起和激活
  10. 学校的论文查重可以查几次?