当前位置:网站首页>2121. 相同元素的间隔之和-哈希表法
2121. 相同元素的间隔之和-哈希表法
2022-06-23 05:48:00 【Mr Gao】
2121. 相同元素的间隔之和
给你一个下标从 0 开始、由 n 个整数组成的数组 arr 。
arr 中两个元素的 间隔 定义为它们下标之间的 绝对差 。更正式地,arr[i] 和 arr[j] 之间的间隔是 |i - j| 。
返回一个长度为 n 的数组 intervals ,其中 intervals[i] 是 arr[i] 和 arr 中每个相同元素(与 arr[i] 的值相同)的 间隔之和 。
注意:|x| 是 x 的绝对值。
示例 1:
输入:arr = [2,1,3,1,2,3,3]
输出:[4,2,7,2,4,4,5]
解释:
- 下标 0 :另一个 2 在下标 4 ,|0 - 4| = 4
- 下标 1 :另一个 1 在下标 3 ,|1 - 3| = 2
- 下标 2 :另两个 3 在下标 5 和 6 ,|2 - 5| + |2 - 6| = 7
- 下标 3 :另一个 1 在下标 1 ,|3 - 1| = 2
- 下标 4 :另一个 2 在下标 0 ,|4 - 0| = 4
- 下标 5 :另两个 3 在下标 2 和 6 ,|5 - 2| + |5 - 6| = 4
- 下标 6 :另两个 3 在下标 2 和 5 ,|6 - 2| + |6 - 5| = 5
示例 2:
输入:arr = [10,5,10,10]
输出:[5,0,3,4]
解释:
- 下标 0 :另两个 10 在下标 2 和 3 ,|0 - 2| + |0 - 3| = 5
- 下标 1 :只有这一个 5 在数组中,所以到相同元素的间隔之和是 0
- 下标 2 :另两个 10 在下标 0 和 3 ,|2 - 0| + |2 - 3| = 3
- 下标 3 :另两个 10 在下标 0 和 2 ,|3 - 0| + |3 - 2| = 4
/** * Note: The returned array must be malloced, assume caller calls free(). */
#define size 100
struct hash{
int val;
int index;
struct hash *next;
};
void add_hash( struct hash *h,int val,int index){
struct hash* p=(struct hash*)malloc(sizeof(struct hash));
p->val=val;
p->index=index;
p->next=h->next;
h->next=p;
}
int find_sum(struct hash *h,int val,int index){
struct hash *p=h->next;
int re=0;
while(p){
if(p->val==val){
re=re+abs(index-p->index);
}
p=p->next;
}
return re;
}
long long* getDistances(int* arr, int arrSize, int* returnSize){
struct hash *h=(struct hash*)malloc(sizeof(struct hash)*size);
long long* re=(long long*)malloc(sizeof(long long)*arrSize);
int i;
for(i=0;i<size;i++){
(h+i)->next=NULL;
}
for(i=0;i<arrSize;i++){
add_hash(h+(arr[i]%size),arr[i],i);
}
for(i=0;i<arrSize;i++){
re[i]=find_sum(h+(arr[i]%size),arr[i],i);
}
*returnSize=arrSize;
return re;
}
边栏推荐
- json转化为proto
- QT中的item views与Item widgets控件的用法总结
- 从 WAN 到 SD-WAN 边缘设备的网络架构
- 下载oss文件并修改文件名
- Haas506 2.0 development tutorial - Advanced Component Library -modem Sim (only supports versions above 2.2)
- Copy and paste of idea without escape
- 如何实现与FDA保持邮件通信安全加密?
- 中台库存中的实仓与虚仓的业务逻辑设计
- C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
- Termux
猜你喜欢
随机推荐
Numerical calculation method chapter7 Calculating eigenvalues and eigenvectors of matrices
xml dtd 记录
Badly placed ()‘s 问题
Qt使用多线程编译项目的方法
光谱共焦的测量原理及厚度测量模式
下载oss文件并修改文件名
haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)
C language stepping on the pit: document coding error, resulting in Base64 Chinese coding error
How to maintain secure encryption of email communication with FDA?
Measurement principle and thickness measurement mode of spectral confocal
system 权限程序不能访问sd卡问题
Haas506 2.0 development tutorial -sntp (only versions above 2.2 are supported)
Easy EDA learning notes 09 esp32-wroom-32e module esp32-devkitc-v4 development board one click download circuit
haas506 2.0开发教程-hota(仅支持2.2以上版本)
bootstrap如何清除浮动的样式
Leetcode notes: Weekly contest 298
开源OAuth2框架 实现SSO单点登录
English grammar_ Adjective comparative - Level 3 change
Coordinate transformation
Illustration Google V8 18: asynchronous programming (I): how does V8 implement micro tasks?









