当前位置:网站首页>"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;
}
};
结果展示:
边栏推荐
猜你喜欢
web容器及IIS --- 中间件渗透方法1
What is a system?
IDEA 注释报红解决
Recursive query single table - single table tree structure - (self-use)
IDEA comment report red solution
Redis实现分布式锁
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
STM32问题合集
Redis implements distributed locks
The distance value between two arrays of LeetCode simple questions
随机推荐
浅识Flutter 基本组件之CheckboxListTile组件
自己的一些思考
[Compilation principle] Lexical analysis program design principle and implementation
C语言从入门到如土——数据的存储
Mysql 45 study notes (twenty-five) MYSQL guarantees high availability
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
Redis 使用 sorted set 做最新评论缓存
Detailed explanation of TCP (3)
A brief introduction to the CheckBox component of the basic components of Flutter
下载jar包的好地方
[Dynamic programming] Maximum sum of consecutive subarrays
STM32 problem collection
Recursive query single table - single table tree structure - (self-use)
【CocosCreator 3.5】CocosCreator get network status
「 每日一练,快乐水题 」1331. 数组序号转换
想从手工测试转岗自动化测试,需要学习哪些技能?
【CocosCreator 3.5】CocosCreator 获取网络状态
Map.Entry理解和应用
安全20220722
Redis uses LIST to cache the latest comments