当前位置:网站首页>LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路
LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路
2022-07-29 03:16:00 【HERODING23】

解题思路:
将数组进行排序,用哈希表进行映射得到每个数对应的序号,然后再重新遍历数组替换数组中的元素即可,代码如下:
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> temp = arr;
sort(temp.begin(), temp.end());
unordered_map<int, int> mp;
for(auto& t : temp) {
if(!mp.count(t)) {
mp[t] = mp.size() + 1;
}
}
for(int i = 0; i < arr.size(); i ++) {
arr[i] = mp[arr[i]];
}
return arr;
}
};
边栏推荐
- C语言程序设计 | 交换二进制数奇偶位(宏实现)
- 简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?
- C traps and defects Chapter 3 semantic "traps" 3.8 operators &, |, and!
- C陷阱与缺陷 第2章 语法“陷阱” 2.6 “悬挂”else引发的问题
- 西瓜书学习第六章---SVM
- Hangao database best practice configuration tool Hg_ BP log collection content
- Navicat new database
- 万字详解 Google Play 上架应用标准包格式 AAB
- Redis configuration cache expiration listening event trigger
- C language programming | exchange binary odd and even bits (macro Implementation)
猜你喜欢

【FreeSwitch开发实践】UniMRCP编译与安装

融云 IM & RTC 能力上新盘点

Navicat new database

C语言基础知识点汇总

July 28, 2022 Gu Yujia's study notes

从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析

Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?

Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.

During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“

Multi table (Association) query of SQL query data
随机推荐
MySQL operation database data error: fatal error encoded during command execution
Shardingsphere's level table practice (III)
"PHP Basics" output approximate value of PI
力扣刷题之分数加减运算(每日一题7/27)
万字详解 Google Play 上架应用标准包格式 AAB
MySQL忘记密码怎么办
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
Sanzi chess (player + computer)
逐步分析类的拆分之案例——五彩斑斓的小球碰撞
C language programming | exchange binary odd and even bits (macro Implementation)
【C】数组
Engineering boy: under 20 years old, ordinary but not mediocre
C traps and defects Chapter 3 semantic "traps" 3.6 Boundary Calculation and asymmetric boundary
Hangao database best practice configuration tool Hg_ BP log collection content
照片比例校正工具:DxO ViewPoint 3 直装版
【FreeSwitch开发实践】UniMRCP编译与安装
Shell programming specifications and variables
国产ERP有没有机会击败SAP ?
C陷阱与缺陷 第3章 语义“陷阱” 3.9 整数溢出
一种简单通用的获取函数栈空间大小的方法