当前位置:网站首页>365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
2022-07-29 05:08:00 【ShowM3TheCode】
离散化处理的思想
这篇讲得不错,核心就是利用哈希表来重新排序
1331. 数组序号转换

代码实现(自解)
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> other = arr;
unordered_map<int, int> _map;
sort(other.begin(), other.end());
for (int i = 0; i < other.size(); i++) {
if (!_map.count(other[i])) {
_map[other[i]] = _map.size() + 1;
}
}
for (int& num : arr) {
num = _map[num];
}
return arr;
}
};
506. 相对名次

代码实现(自解)
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
vector<int> ref = score;
unordered_map<int, int> _map;
vector<string> ans;
sort(ref.begin(), ref.end(), greater<int>());
for (int i = 0; i < ref.size(); i++) {
_map[ref[i]] = _map.size() + 1;
}
for (int candidate : score) {
switch(_map[candidate]) {
case 1:
ans.push_back("Gold Medal");
break;
case 2:
ans.push_back("Silver Medal");
break;
case 3:
ans.push_back("Bronze Medal");
break;
default:
ans.push_back(to_string(_map[candidate]));
}
}
return ans;
}
};
边栏推荐
- Webrtc audio anti weak network technology (Part 2)
- Google gtest事件机制
- Pivot table of odoo development tutorial
- MySQL sorts the queried result set according to the specified sequence
- js(forEach)出现return无法结束函数的解决方法
- What if the computer cannot open excel? The solution of Excel not opening
- The latest tank battle 2022 - Notes on the whole development -2
- Soft link & hard link
- Ros1 dead chicken data is stored in txt and SQLite
- Activity workflow table structure learning
猜你喜欢

SM integration is as simple as before, and the steps are clear (detailed)

Unity3D - 物体太远看不见的问题

Scikit learn -- steps and understanding of machine learning application development

Getting started with solidity

On AspectJ framework

The method and detailed code of automatically pop-up and QQ group when players visit the website

Soft link & hard link

开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开

ARFoundation从零开始3-创建ARFoundation项目

What if the office prompts that the system configuration cannot run?
随机推荐
【config】配置数组参数
源码编译pytorch坑
How to install Office2010 installation package? How to install Office2010 installation package on computer
How to add traffic statistics codes to the legendary Development Zone website
osg3.6.5编译freetype失败
手写学生管理系统
Open the tutorial of adding and modifying automatically playing music on the open zone website
SM integration is as simple as before, and the steps are clear (detailed)
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
什么是_GLIBCXX_VISIBILITY(default)
Scikit learn -- steps and understanding of machine learning application development
Database course design of online assistant teaching platform for high school chemistry
ODOO开发教程之图表
Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
最新坦克大战2022-全程开发笔记-1
Modification of annotation based three-tier project and the way of adding package scanning
On AspectJ framework
C language handwritten qq-ai version
缓存穿透、缓存击穿、缓存雪崩以及解决方法