当前位置:网站首页>398. 随机数索引-哈希表法
398. 随机数索引-哈希表法
2022-07-23 16:30:00 【Mr Gao】
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 之一。每个索引的返回概率应该相等。
解题代码如下:
typedef struct {
long long index;
long long val;
struct Solution *next;
} Solution;
Solution* solutionCreate(int* nums, int numsSize) {
Solution *l=(Solution *)malloc(sizeof(Solution)*10000);
int i;
for(i=0;i<10000;i++){
(l+i)->next=NULL;
}
for(i=0;i<numsSize;i++){
Solution *p=(Solution *)malloc(sizeof(Solution));
p->index=i;
p->val=nums[i];
p->next=(l+abs(nums[i])%10000)->next;
(l+abs(nums[i])%10000)->next=p;
}
return l;
}
int solutionPick(Solution* obj, int target) {
Solution * p=(obj+abs(target)%10000)->next;
int count=0;
int pc=rand();
while(p){
if(p->val==target){
count++;
}
p=p->next;
}
pc=pc%count;
int cz=0;
p=(obj+abs(target)%10000)->next;
while(p){
if(p->val==target){
if(cz==pc){
return p->index;
}
cz++;
}
p=p->next;
}
return -1;
}
void solutionFree(Solution* obj) {
}
/** * Your Solution struct will be instantiated and called as such: * Solution* obj = solutionCreate(nums, numsSize); * int param_1 = solutionPick(obj, target); * solutionFree(obj); */
边栏推荐
- How does Apache, the world's largest open source foundation, work?
- 怎么将word中的times new roman的双引号替换成宋体双引号
- Common problems of sklearn classifier
- 【游戏建模模型制作全流程】3ds Max和ZBrush制作无线电接收器
- Alliance DAO创始人:100+Web3基础设施及Dapp创业清单
- How to capture the analyst rating data of Sina Financial Data Center?
- 【重磅】聚焦券商终端业务,博睿数据发布新一代券商终端核心业务体验可观测平台
- 数字信号处理实验(一)
- 如何抓取新浪财经数据中心分析师评级数据?
- 有人是靠自学建模找到工作的吗?千万别让这些思维害了你
猜你喜欢

Problems encountered in the project and Solutions
![[sharing 3D modeling and production skills] how ZBrush turns pictures into relief models](/img/fc/c400821c07ea43576a14a30d96183f.png)
[sharing 3D modeling and production skills] how ZBrush turns pictures into relief models

How to become a modeler? Which is more popular, industrial modeling or game modeling?

《通信软件开发与应用》课程结业报告

入行3D建模有前景吗?就业高薪有保障还是副业接单赚钱多

Alliance DAO创始人:100+Web3基础设施及Dapp创业清单

【JZOF】13機器人的運動範圍

自学3D建模能不能成功?自学能就业吗?

Time frequency domain analysis of 20220721 integral link

kubectl 创建 Pod 背后到底发生了什么?
随机推荐
Flame Graphs 火焰图安装与使用
Crack WiFi password with Kail
UAV circulating an unknown target under a GPS deniedenvironment with range only measurements translation
Major optimization of openim - Message loading on demand, consistent cache, uniapp Publishing
rhcsa笔记七
ResponseBodyAdvice接口使用导致的报错及解决
Deep learning learning record - update of learning rate of optimizer
如何成为建模师?工业建模和游戏建模哪一个更加吃香?
Flutter 运行模式
kubectl 创建 Pod 背后到底发生了什么?
零一的昔日织-2022
Block encryption mode ECB, CBC, PCBC, CFB, OFB, CTR
[easy to understand] relational schema paradigm decomposition tutorial 3NF and BCNF formula! Xiaobai can also understand "suggestions collection"
Rhcsa note 4
类的基础
C language · structure (Introduction to linear table)
怎么将word中的times new roman的双引号替换成宋体双引号
Flutter operation mode
Is learning next generation modeling a good scene or a good role? Choose the right profession and pay more than half
[jzof] 13 motion range of robot