当前位置:网站首页>Leetcode-1331: array ordinal conversion
Leetcode-1331: array ordinal conversion
2022-07-29 06:57:00 【Chrysanthemum headed bat】
leetcode-1331: Array number conversion
subject
Give you an array of integers arr , Please replace each element in the array with their ordinal number after sorting .
The serial number represents how big an element is . The rules for serial numbers are as follows :
The serial number from 1 Numbered starting .
The bigger an element is , So the bigger the serial number . If two elements are equal , So they have the same serial number .
The serial number of each number should be as small as possible .
Example 1:
Input :arr = [40,10,20,30]
Output :[4,1,2,3]
explain :40 It's the biggest element . 10 It's the smallest element . 20 It's the second smallest number . 30 It's the third smallest number .
Example 2:
Input :arr = [100,100,100]
Output :[1,1,1]
explain : All elements have the same sequence number .
Example 3:
Input :arr = [37,12,28,9,100,56,80,5,12]
Output :[5,3,4,2,8,6,7,1,3]
Problem solving
Method 1 : Sort + Hash
Sort , And record the sequence number of each number with hash
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;
}
};
边栏推荐
- 游戏资产的革命
- NLP-分词
- C语言数据类型
- Summary of 2022 SQL classic interview questions (with analysis)
- 数据库多表查询 联合查询 增删改查
- 王树尧老师运筹学课程笔记 04 线性代数基础
- 吴恩达老师机器学习课程笔记 05 Octave教程
- 王树尧老师运筹学课程笔记 03 KKT定理
- API for using the new date class of instant
- Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
猜你喜欢

LDAP brief description and unified authentication description

【冷冻电镜|论文阅读】子断层平均 M 软件解读:Multi-particle cryo-EM refinement with M

吴恩达老师机器学习课程笔记 02 单变量线性回归

N2 interface of 5g control plane protocol

【flask入门系列】Flask-SQLAlchemy的安装与配置

leetcode-592:分数加减运算

Software definition boundary SDP

Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice

Neuralcf neural collaborative filtering network

【经验】通过跳板机远程连接内网服务器的相关配置
随机推荐
SDN topology discovery principle
Mutual conversion between Base64 and file
剑指 Offer II 115:重建序列
王树尧老师运筹学课程笔记 09 线性规划与单纯形法(单纯形表的应用)
数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
模拟卷Leetcode【普通】150. 逆波兰表达式求值
吴恩达老师机器学习课程笔记 04 多元线性回归
Ping principle
Ali gave several SQL messages and asked how many tree search operations need to be performed?
【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
矩阵分解与梯度下降
C language memory stack and heap usage
Phantom reference virtual reference code demonstration
Analysis of four isolation levels of MySQL things
如何优雅的写 Controller 层代码?
Database multi table query joint query add delete modify query
vim文本编辑器的一些使用小技巧
吴恩达老师机器学习课程笔记 02 单变量线性回归
【笔记】The art of research - (讲好故事和论点)
竣达技术 | 适用于”日月元”品牌UPS微信云监控卡