当前位置:网站首页>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;
}
};
边栏推荐
- 模拟卷Leetcode【普通】061. 旋转链表
- 竣达技术 | 适用于”日月元”品牌UPS微信云监控卡
- Idea cannot find a database solution
- Share some tips for better code, smooth coding and improve efficiency
- 基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
- Understanding of access, hybrid and trunk modes
- Mutual conversion between Base64 and file
- Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
- 数据库多表查询 联合查询 增删改查
- Loss function -- cross entropy loss function
猜你喜欢

联邦学习后门攻击总结(2019-2022)

NLP word segmentation

Loss function -- cross entropy loss function

CNN convolutional neural network

NeuralCF-神经协同过滤网络

MySql基础知识(高频面试题)

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

Analysis of four isolation levels of MySQL things

How to write controller layer code gracefully?

王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)
随机推荐
Etcd principle
【备忘】关于ssh为什么会失败的原因总结?下次记得来找。
SDN topology discovery principle
Actual combat! Talk about how to solve the deep paging problem of MySQL
Share some tips for better code, smooth coding and improve efficiency
Unity探索地块通路设计分析 & 流程+代码具体实现
剑指 Offer II 115:重建序列
mysql查询区分大小写
【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
5g service interface and reference point
微信小程序的反编译
【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
Embedding understanding + code
Analysis of four isolation levels of MySQL things
数据库系统概述
模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
Teacher wangshuyao's notes on operations research 05 linear programming and simplex method (concept, modeling, standard type)
Summary of 2022 SQL classic interview questions (with analysis)
Recurrent neural network RNN