当前位置:网站首页>LeetCode·每日一题·1331.数组序号转换·离散化
LeetCode·每日一题·1331.数组序号转换·离散化
2022-07-28 12:02:00 【小迅想变强】
链接:https://leetcode.cn/problems/rank-transform-of-an-array/solution/by-xun-ge-v-njcw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目

示例

思路
对应哈希算法还不是特别熟悉的,可以看下方链接学习:
哈希算法详解
解题思路
对于本题,其实就是对数组元素进行离散化处理,存在的意义在于,当我们遇到值域非常大的数组时,我们想对这个数组进行处理,但是在处理过程中我们只关心元素的大小关系,并不关心实际值,但是值域非常大又会影响我们使用一些好的数据结构处理,比如线段树。。。那么此时就需要对数组元素进行离散化。比如数组[1,22,333,4444,55555],在我们只关心元素大小时,其实和[1,2,3,4,5]是等价的。
具体实现
先对数组元素进行升序处理。然后将数组元素作为哈希key值,存入哈希表,并且打上标记,再遍历原始数组,将对应数组离散化结果存入对应位置。
代码
typedef struct {//定义散列表
int key;
int val;
UT_hash_handle hh;
} HashItem;
static inline int cmp(const void *pa, const void *pb) {//升序子函数
return *(int *)pa - *(int *)pb;
}
int* arrayRankTransform(int* arr, int arrSize, int* returnSize) {
int *sortedArr = (int *)malloc(sizeof(int) * arrSize);
int *ans = (int *)malloc(sizeof(int) * arrSize);
memcpy(sortedArr, arr, sizeof(int) * arrSize);
qsort(sortedArr, arrSize, sizeof(int), cmp);//初始化变量
HashItem *ranks = NULL;//定义哈希表头结点
int n = 1;
for (int i = 0; i < arrSize; i++) {//遍历升序后的数组,并加入到哈希表中
HashItem *pEntry = NULL;//定义哈希元素
HASH_FIND_INT(ranks, &sortedArr[i], pEntry);//查询元素是否已经存在哈希表中
if (pEntry == NULL) {//不存在,申请节点,存入哈希表
pEntry = (HashItem *)malloc(sizeof(HashItem));
pEntry->key = sortedArr[i];
pEntry->val = n++;//打上标记
HASH_ADD_INT(ranks, key, pEntry);//添加到哈希表
}//存在时因为相同元素标记相同,所以不需要处理
}
for (int i = 0; i < arrSize; i++) {//遍历原始数组
HashItem *pEntry = NULL;
HASH_FIND_INT(ranks, &arr[i], pEntry);//寻找原始数组元素对应的标记
ans[i] = pEntry->val;
}
*returnSize = arrSize;
HashItem *cur, *tmp;
HASH_ITER(hh, ranks, cur, tmp) {//销毁哈希表
HASH_DEL(ranks, cur);
free(cur);
}
free(sortedArr);
return ans;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/rank-transform-of-an-array/solution/by-xun-ge-v-njcw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。时间空间复杂度

边栏推荐
- BiliBili Yang Zhou: above efficiency, efficient delivery
- MySQL is always installed unsuccessfully. Just do it like this
- Xampp Chinese tutorial guide
- Black cat takes you to learn UFS agreement part 2: Interpretation of UFS related terms
- Quick read in
- [July 5 event preview] Flink Summit
- Black cat takes you to learn EMMC Protocol Part 26: hardware reset operation of EMMC (h/w reset)
- Protobuf data exchange format
- How to improve deep learning performance?
- [embedded C foundation] Part 5: original code / inverse code / complement code
猜你喜欢

Kotlin是如何帮助你避免内存泄漏的?

Leetcode 42. rainwater connection

Flexpro software: measurement data analysis in production, research and development

What if the right button of win11 start menu doesn't respond

Machine learning practice - integrated learning-23

Jetpack Compose 完全脱离 View 系统了吗?
![[embedded explanation] key scanning based on finite state machine and stm32](/img/ce/cc3f959a4e4f5b22e2c711ea887ad7.png)
[embedded explanation] key scanning based on finite state machine and stm32

Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho

2020-12-27
![[basic teaching of Bi design] detailed explanation of OLED screen use - single chip microcomputer Internet of things](/img/76/820d4e357206f936b33da92a5e2b5b.png)
[basic teaching of Bi design] detailed explanation of OLED screen use - single chip microcomputer Internet of things
随机推荐
Pointnet++ Chinese Translation
9、 Kubernetes configuration and storage
Full disclosure! Huawei cloud distributed cloud native technology and Practice
Zurich Federal Institute of technology | reference based image super resolution with deformable attention transformer (eccv2022))
机器学习实战-逻辑回归-19
[Bi design teaching] STM32 and FreeRTOS realize low power consumption
《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!
[embedded C foundation] Part 9: basic usage of C language pointer
What if the win11 folder cannot be opened
Leetcode remove element & move zero
黑猫带你学eMMC协议第27篇:什么是eMMC的动态容量(Dynamic Capacity)?
Databinding+livedata can easily realize skin changing without restart
Remove the plug-in of category in WordPress link
Monotonic stack
10、 Kubernetes scheduling principle
BA autoboot plug-in of uniapp application boot
Databinding+LiveData轻松实现无重启换肤
[FPGA]: ISE generates MCS file and burning process
Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
Redefinition problem of defining int i variable in C for loop