当前位置:网站首页>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);
}
}边栏推荐
- [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)
- Linux (centos7) installer mysql - 5.7
- 力扣解法汇总953-验证外星语词典
- How to maximize the use of various matching methods—— Google SEM
- [adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
- How to locate keywords to make advertising accurate.
- UE4\UE5触摸屏touch事件:单指、双指
- 力扣解法汇总1022-从根到叶的二进制数之和
- How can low code platforms improve cost effectiveness?
- 力扣解法汇总462-最少移动次数使数组元素相等 II
猜你喜欢

Alicloud OSS file upload system

Graphical data analysis | data analysis tool map

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

Installing MySQL version 5.5 database for Linux (centos6)

Why do we use Google search ads?

ACL 2022 | 预训练语言模型和图文模型的强强联合

Ozzanmation action system based on SSE

MySQL advanced knowledge points

代理与反射(二)

Is the bidding price fixed for each click?
随机推荐
MySQL高级部分知识点
How to improve the advertising rating of advertising, that is, the quality score?
android html5页面加载缓存优化
How to stop anti-virus software from blocking a web page? Take gdata as an example
Advantages of Google ads
Design principle [Demeter's Law]
力扣解法汇总1728-猫和老鼠 II
Explore performance optimization! Performance improvement from 2 months to 4 hours!
[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)
php开发 博客系统的公告模块的建立和引入
Make ads more relevant by searching for additional information about ads
How WPS inserts a directory and the operating steps for quickly inserting a directory
ACL 2022 | 预训练语言模型和图文模型的强强联合
力扣解法汇总942-增减字符串匹配
PHP development 09 article module deletion and article classification writing
力扣解法汇总953-验证外星语词典
Operating mechanism of Google ads bidding
ozzanimation-基于sse的动作系统
Installing mysql-5.7 for Linux (centos7)
CVPR2022 | iFS-RCNN:一种增量小样本实例分割器