当前位置:网站首页>力扣解法汇总398-随机数索引
力扣解法汇总398-随机数索引
2022-06-12 02:04:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引。 您可以假设给定的数字一定存在于数组中。
注意:
数组大小可能非常大。 使用太多额外空间的解决方案将不会通过测试。
示例:
int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);
// pick(3) 应该返回索引 2,3 或者 4。每个索引的返回概率应该相等。
solution.pick(3);
// pick(1) 应该返回 0。因为只有nums[0]等于1。
solution.pick(1);
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/random-pick-index
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 用map的key装载每一个值,value为其index值的集合。 * pick时,取List然后随机取其中的值即可。
代码:
public class Solution398 {
Map<Integer, List<Integer>> map = new HashMap<>();
Random random = new Random();
public Solution398(int[] nums) {
for (int i = 0; i < nums.length; i++) {
List<Integer> list = map.get(nums[i]);
if (list == null) {
list = new ArrayList<>();
map.put(nums[i], list);
}
list.add(i);
}
}
public int pick(int target) {
List<Integer> integers = map.get(target);
int index = random.nextInt(integers.size());
return integers.get(index);
}
}边栏推荐
- [adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
- Smartbi helps you solve the problem of losing high-value customers
- 商城开发知识点
- MySQL高级部分知识点
- Educational knowledge and ability test questions of primary and secondary school educational endowment examination in the second half of 2019 (middle school) - subjective questions
- 力扣解法汇总821-字符的最短距离
- 广泛匹配修饰符符号已经被弃用,请勿使用
- lua 函数
- Huawei intermodal game or application review rejected: the application detected payment servicecatalog:x6
- ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
猜你喜欢

Graphic data analysis | data cleaning and pretreatment

自适应搜索广告有哪些优势?

Don't miss it! Five large data visualization screens that HR must collect

商城开发知识点

Knowledge points of mall development

入手Ticwatch2

Installing MySQL version 5.5 database for Linux (centos6)

ozzanimation-基於sse的動作系統

Introduction to SVM

Smartbi helps you solve the problem of losing high-value customers
随机推荐
入手Ticwatch2
LeetCode Algorithm 997. 找到小镇的法官
Implementation scheme of iteration and combination pattern for general tree structure
How to automatically color cells in Excel
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
Three main factors determining advertising quality
php开发09 文章模块的删除和文章分类编写
How can low code platforms improve cost effectiveness?
力扣解法汇总699-掉落的方块
如何最大化的利用各种匹配方式? ——Google SEM
Graphic data analysis | business cognition and data exploration
kali安装empire过程中遇到的各种报错解决方案
Explore performance optimization! Performance improvement from 2 months to 4 hours!
大一下期:学习总结
Google Ads 竞价的运作机制
力扣解法汇总450-删除二叉搜索树中的节点
Design principle [Demeter's Law]
初探性能优化!从2个月到4小时的性能提升!
PHP development 09 article module deletion and article classification writing
The establishment and introduction of the announcement module of PHP development blog system