当前位置:网站首页>"A daily practice, happy water problem" 1331. Array serial number conversion
"A daily practice, happy water problem" 1331. Array serial number conversion
2022-07-31 03:32:00 【who eats mints】
力扣原题:
*题目简述:
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号.
序号代表了一个元素有多大.序号编号的规则如下:
序号从 1 开始编号.
一个元素越大,那么序号越大.如果两个元素相等,那么它们的序号相同.
每个数字的序号都应该尽可能地小.
*解题思路:
- 哈希大法好;
- Use hashing to deduplicate and sort;
- Then complete the replacement;
- over;
*C++代码:
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> res(arr);
sort(arr.begin(), arr.end());
unordered_map<int, int> umap;
int j = 0;
for(auto & i : arr)
{
if(umap.count(i) == 0)
{
umap[i] = ++j;
}
}
int n = res.size();
for(int i = 0; i < n; i++)
{
res[i] = umap[res[i]];
}
return res;
}
};
结果展示:
边栏推荐
- addressable in Golang
- IDEA常用快捷键与插件
- With 7 years of experience, how can functional test engineers improve their abilities step by step?
- Safety 20220712
- 【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
- 进程间通信
- Safety 20220722
- Detailed explanation of TCP and UDP
- Addition and Subtraction of Scores in LeetCode Medium Questions
- Redis 使用 sorted set 做最新评论缓存
猜你喜欢
LeetCode每日一练 —— OR36 链表的回文结构
What is a system?
No qualifying bean of type 问题
Just debuted "Fight to Fame", safety and comfort are not lost
[Dynamic programming] Maximum sum of consecutive subarrays
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
「 每日一练,快乐水题 」1331. 数组序号转换
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
QML的使用
随机推荐
Database implements distributed locks
Safety 20220712
Key Technologies of Interface Testing
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
The BP neural network
TCP详解(三)
MP使用时的几个常见报错
LeetCode简单题之找到和最大的长度为 K 的子序列
A brief introduction to the CheckboxListTile component of the basic components of Flutter
Implementation of a sequence table
(线段树) 基础线段树常见问题总结
注解用法含义
[C language] General method of expression evaluation
Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
Map.Entry理解和应用
安全20220722
【CocosCreator 3.5】CocosCreator get network status
SocialFi 何以成就 Web3 去中心化社交未来
MultipartFile文件上传
LeetCode每日一练 —— OR36 链表的回文结构