当前位置:网站首页>【C语言刷LeetCode】1331. 数组序号转换(E)
【C语言刷LeetCode】1331. 数组序号转换(E)
2022-07-30 02:02:00 【kinbo88】
【
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
序号从 1 开始编号。
一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。
每个数字的序号都应该尽可能地小。
示例 1:
输入:arr = [40,10,20,30]
输出:[4,1,2,3]
解释:40 是最大的元素。 10 是最小的元素。 20 是第二小的数字。 30 是第三小的数字。
示例 2:
输入:arr = [100,100,100]
输出:[1,1,1]
解释:所有元素有相同的序号。
示例 3:
输入:arr = [37,12,28,9,100,56,80,5,12]
输出:[5,3,4,2,8,6,7,1,3]
提示:
0 <= arr.length <= 105
-109 <= arr[i] <= 109
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rank-transform-of-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
】
一个简单题,居然要用UTHASH 了:
typedef struct {
int key;
int val;
UT_hash_handle hh;
} UTHASH;
int Cmp (UTHASH *a, UTHASH *b) {
return a->key - b->key;
}
int* arrayRankTransform(int* arr, int arrSize, int* returnSize){
UTHASH *mymap = NULL;
int i;
int *retarr = malloc(sizeof(int) * arrSize);
for (i = 0; i < arrSize; i++) {
UTHASH *find = NULL;
HASH_FIND_INT(mymap, &arr[i], find);
if (find == NULL) {
UTHASH *new = malloc(sizeof(UTHASH));
new->key = arr[i];
HASH_ADD_INT(mymap, key, new);
}
}
HASH_SORT(mymap, Cmp);
UTHASH *cur, *tmp;
int idx = 1; // 序号
HASH_ITER(hh, mymap, cur, tmp) {
cur->val = idx;
idx++;
}
for (i = 0; i < arrSize; i++) {
UTHASH *findn = NULL;
HASH_FIND_INT(mymap, &arr[i], findn); // 一定能找到
retarr[i] = findn->val;
}
*returnSize = arrSize;
return retarr;
}边栏推荐
- English grammar_indefinite pronouns -some & any
- 把@Transactional事务注解用到如此炉火纯青,真的强!
- Sublime does background transparency and column editor
- SwiftUI SQLite数据库存储使用教程大合集(2022年版)
- MPLS VPN跨域-optionC2
- 解决:Error while adding the mapper ‘interface to configuration. Error parsing Mapper XML
- Teach you how to achieve a flowing gradient border
- MIT6.S081 Summary
- LeetCode每日一题(874. Walking Robot Simulation)
- 信息系统项目管理师核心考点(五十四)配置项分类、状态与版本
猜你喜欢

Type-C边充电边OTG芯片——LDR6028A

LeetCode 2352. 相等行列对

ButtonStyle, MaterialStateProperty learned by flutter

JS develops 3D modeling software

CAPL中的键值对(hash)数据类型

Type-C charging and OTG chip - LDR6028A

vscode 工作区配置插件 配置不同工作环境

把@Transactional事务注解用到如此炉火纯青,真的强!

postgresql日常运维技能,适合初学者

LeetCode 2342. Digital and equal number of one of the biggest and
随机推荐
【MySQL总结】
Tibetan Mapping
数据流图、数据字典
Implementation of Portable VR in Unity
[Microservice~Nacos] Nacos service provider and service consumer
JNPF3.4.2 system upgrade announcement
面试题:手写Promise
二叉搜索树
LeetCode 2348. 全 0 子数组的数目
fluttter学习之ButtonStyle 、MaterialStateProperty
Docker一键安装MySQL
go jwt use
【MySQL必知必会】 范式 | ER模型
基于燃压缩空气储能系统的零碳微能源互联网优化调度(Matlab代码实现)
Graphical LeetCode - 593. Valid Squares (Difficulty: Moderate)
【Vmware NSX-V基本架构及组件安装】
【微服务~Nacos】Nacos之配置中心
go grpc 自定义拦截器
sqlserver 多行合并成一行
binary search tree