当前位置:网站首页>leetcode-1331:数组序号转换
leetcode-1331:数组序号转换
2022-07-29 05:38:00 【菊头蝙蝠】
题目
给你一个整数数组 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]
解题
方法一:排序+哈希
排序,并用哈希记录每个数的序号即可
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
if(arr.size()==0) return {
};
vector<int> tmp=arr;
sort(tmp.begin(),tmp.end());
unordered_map<int,int> mp;
int index=1;
for(int num:tmp){
if(!mp.count(num)) mp[num]=index++;
}
vector<int> res;
for(int num:arr) res.push_back(mp[num]);
return res;
}
};
边栏推荐
- 多线程并发下的指令重排问题
- Actual combat! Talk about how to solve the deep paging problem of MySQL
- SDN拓扑发现原理
- Enterprise manager cannot connect to the database instance in Oracle10g solution
- IDEA中实现Mapper接口到映射文件xml的跳转
- 王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
- Ali gave several SQL messages and asked how many tree search operations need to be performed?
- 网络工具中的“瑞士军刀”-nc
- 新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
- LDAP brief description and unified authentication description
猜你喜欢

CDM—码分复用(简单易懂)

为什么5G N2接口控制面使用SCTP协议?

MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear

Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra

循环神经网络RNN

Shallow reading of shared lock source code of abstractqueuedsynchronizer (AQS)

Jetpack Compose 中的键盘处理

5G控制面协议之N2接口

数据单位:位、字节、字、字长

Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
随机推荐
吴恩达老师机器学习课程笔记 00 写在前面
C language memory stack and heap usage
Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers
实战!聊聊如何解决MySQL深分页问题
王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
王树尧老师运筹学课程笔记 04 线性代数基础
吴恩达老师机器学习课程笔记 04 多元线性回归
Talk about tcp/ip protocol? And the role of each layer?
【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)
王树尧老师运筹学课程笔记 01 导学与绪论
Neuralcf neural collaborative filtering network
【讲座笔记】如何在稀烂的数据中做深度学习?
Use of PDO
MySql基础知识(高频面试题)
Thinkphp5 frequently asked questions
Ping principle
How to use SFTP command to access SFTP server on the development board
【经验】通过跳板机远程连接内网服务器的相关配置
SS command details