当前位置:网站首页>Force deduction solution summary 398 random number index
Force deduction solution summary 398 random number index
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Given an array of integers that may contain repeating elements , The index of a given number is required to be output randomly . You can assume that a given number must exist in an array .
Be careful :
The array size can be very large . Solutions that use too much extra space will not pass the test .
Example :
int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);
// pick(3) The index should be returned 2,3 perhaps 4. The return probability of each index should be equal .
solution.pick(3);
// pick(1) Should return to 0. Because only nums[0] be equal to 1.
solution.pick(1);
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/random-pick-index
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * use map Of key Load each value ,value For its index Collection of values . * pick when , take List Then take the value randomly .
Code :
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);
}
}边栏推荐
- 力扣解法汇总449-序列化和反序列化二叉搜索树
- Various error reporting solutions encountered by Kali during Empire installation
- 如何提高广告的广告评级,也就是质量得分?
- 力扣解法汇总965-单值二叉树
- 力扣解法汇总面试题 17.11-单词距离
- 如何定位关键词使得广告精准投放。
- 力扣解法汇总953-验证外星语词典
- 力扣解法汇总1037-有效的回旋镖
- Linux(CentOS7)安装MySQL-5.7版本
- Summary of force deduction method 417- Pacific Atlantic current problems
猜你喜欢

BaseDexClassLoader那些事

通用树形结构的迭代与组合模式实现方案

Smartbi helps you solve the problem of losing high-value customers

The most comprehensive redis transaction control in 2022 (with illustration)

超图倾斜数据合并根节点后转3dtiles

Google Ads 竞价的运作机制

SQL calculates KS, AUC, IV, psi and other risk control model indicators

为什么我们要使用谷歌搜索广告?

Ozzanmation action system based on SSE

php开发 博客系统的公告模块的建立和引入
随机推荐
力扣编程题-解法汇总
UE4\UE5触摸屏touch事件:单指、双指
The establishment and introduction of the announcement module of PHP development blog system
初探性能优化!从2个月到4小时的性能提升!
力扣解法汇总面试题 01.05. 一次编辑
What insurance products can you buy at the age of 60?
android html5页面加载缓存优化
Force deduction solution summary 905- array sorted by parity
C asynchronous programming from simple to deep (III) details awaiter
Niuke monthly race 14- simple counting
"China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
LeetCode Algorithm 1791. Find the central node of the star chart
How can low code platforms improve cost effectiveness?
What are the advantages of adaptive search advertising?
Google 搜索广告系列设置前有哪些准备工作?
力扣解法汇总1022-从根到叶的二进制数之和
力扣解法汇总427-建立四叉树
入手Ticwatch2
MySQL advanced knowledge points
Introduction to SVM