当前位置:网站首页>「 每日一练,快乐水题 」1331. 数组序号转换
「 每日一练,快乐水题 」1331. 数组序号转换
2022-07-31 03:18:00 【谁吃薄荷糖】
力扣原题:
*题目简述:
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
序号从 1 开始编号。
一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。
每个数字的序号都应该尽可能地小。
*解题思路:
- 哈希大法好;
- 利用哈希大法来去重与排序;
- 然后完成替换即可;
- 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;
}
};
结果展示:
边栏推荐
- VS QT——ui不显示新添加成员(控件)||代码无提示
- 2022 Nioke Multi-School League Game 4 Solution
- Office automation case: how to automatically generate period data?
- Ambiguous method call.both
- 【C语言】进制转换一般方法
- Compile Hudi
- SQL injection Less47 (error injection) and Less49 (time blind injection)
- TCP/IP four-layer model
- Discussion on Service Commitment of Class Objects under Multithreading
- Annotation usage meaning
猜你喜欢
IDEA 注释报红解决
10 Permission introduction
TCP详解(二)
什么是系统?
Recursive query single table - single table tree structure - (self-use)
Getting Started with CefSharp - winform
浅识Flutter 基本组件之CheckboxListTile组件
刚出道“一战成名”,安全、舒适一个不落
[Dynamic programming] Maximum sum of consecutive subarrays
Mysql 45 study notes (twenty-five) MYSQL guarantees high availability
随机推荐
PMP微信群日常习题
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
Golang中的addressable
Getting Started with CefSharp - winform
数据库实现分布式锁
TCP和UDP详解
Mysql 45 study notes (twenty-four) MYSQL master-slave consistency
观察者模式
Detailed explanation of TCP (1)
Chapter 9 SVM Practice
Automation strategies for legacy systems
Graphical lower_bound & upper_bound
遗留系统的自动化策略
IDEA comment report red solution
什么是系统?
Implementation of a sequence table
Several common errors when using MP
TCP/IP four-layer model
IIR滤波器和FIR滤波器
接口测试关键技术