当前位置:网站首页>"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;
}
};
结果展示:

边栏推荐
猜你喜欢

SIP协议标准和实现机制

Detailed explanation of TCP (3)

Redis实现分布式锁

【编译原理】递归下降语法分析设计原理与实现

Zotero如何删除自动生成的标签

The application and practice of mid-to-platform brand advertising platform

TCP和UDP详解

With 7 years of experience, how can functional test engineers improve their abilities step by step?

web容器及IIS --- 中间件渗透方法1

type_traits元编程库学习
随机推荐
Component pass value provide/inject
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
安全20220712
A brief introduction to the showDatePicker method of the basic components of Flutter
【HCIP】ISIS
Redis 使用 sorted set 做最新评论缓存
type_traits元编程库学习
识Flutter 基本组件之showTimePicker 方法
els 方块向右移动边界判断、向下加速
WebSocket Session为null
Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
安全20220722
VS QT - ui does not display newly added members (controls) || code is silent
【动态规划】连续子数组的最大和
遗留系统的自动化策略
Difference between unallocated blocks and unused blocks in database files
Redis 使用LIST做最新评论缓存
The use of beforeDestroy and destroyed
想从手工测试转岗自动化测试,需要学习哪些技能?
TCP详解(二)