当前位置:网站首页>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;
}
};
边栏推荐
- 为什么5G N2接口控制面使用SCTP协议?
- C语言内存-栈与堆使用
- 多线程并发下的指令重排问题
- CNN-卷积神经网络
- Condition 条件对象源码浅读
- 【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging
- 王树尧老师运筹学课程笔记 01 导学与绪论
- Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)
- Best example of amortized cost
- 偏向锁、轻量级锁测试工具类级相关命令
猜你喜欢

5g service interface and reference point

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

Embedding理解+代码

Condition 条件对象源码浅读

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

The core of openresty and cosocket

C语言数据类型

矩阵分解与梯度下降

如何画出优秀的架构图

10 frequently asked JVM questions in interviews
随机推荐
【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
5G控制面协议之N2接口
数据单位:位、字节、字、字长
N2 interface of 5g control plane protocol
王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
Why does 5g N2 interface control plane use SCTP protocol?
Recurrent neural network RNN
基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
王树尧老师运筹学课程笔记 01 导学与绪论
LDAP简述及统一认证说明
【讲座笔记】如何在稀烂的数据中做深度学习?
【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
Instruction rearrangement under multithreading concurrency
网络工具中的“瑞士军刀”-nc
Actual combat! Talk about how to solve the deep paging problem of MySQL
STP spanning tree principle and example of election rules
Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers
吴恩达老师机器学习课程笔记 03 线性代数回顾
吴恩达老师机器学习课程笔记 04 多元线性回归
王树尧老师运筹学课程笔记 00 写在前面