当前位置:网站首页>力扣每日一题-第26天-506.相对名次
力扣每日一题-第26天-506.相对名次
2022-06-25 00:01:00 【重邮研究森】
2022.6.24今天你刷题了吗?
题目:
给你一个长度为 n 的整数数组 score ,其中 score[i] 是第 i 位运动员在比赛中的得分。所有得分都 互不相同 。
运动员将根据得分 决定名次 ,其中名次第 1 的运动员得分最高,名次第 2 的运动员得分第 2 高,依此类推。运动员的名次决定了他们的获奖情况:
名次第 1 的运动员获金牌 "Gold Medal" 。
名次第 2 的运动员获银牌 "Silver Medal" 。
名次第 3 的运动员获铜牌 "Bronze Medal" 。
从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 "x")。
使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。
分析:
给你一个数组,前三大的就写“金牌”,“银牌”,“铜牌”,其他的就写排名,例如排第四就写4,排第五就写5.注意这样返回时必须根据给定数组的顺序来返回。
思路:我们先把原数组取反,利用sort和reverse。然后两个for循环遍历查看当数组元素相等时,把它的原始下标记录,然后给返回字符串对应下标位置记录返回值
解析:
1.暴力求解
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
vector<int>vec(score.size());
int cnt = 0;
vector<string>str(score.size());
int idx = 0;
for (int i = 0; i < score.size(); i++)
{
vec[i] = score[i];
}
sort(vec.begin(), vec.end());
string s;
reverse(vec.begin(), vec.end());
for (auto i = 0; i < vec.size(); i++)
{
for (auto j = 0; j < score.size(); j++)
{
if (vec[i] == score[j])
{
cnt++;
if (cnt == 1)
{
str[j]= "Gold Medal";
}
else if (cnt == 2)
{
str[j] ="Silver Medal";
}
else if (cnt == 3)
{
str[j] = "Bronze Medal";
}
else
{
str[j] = to_string(cnt);
}
}
}
}
return str;
}
};
2.哈希表
这里的思路是,利用一个哈希表来记录原始数据的下标,且在记录时也对原始数据进行了排序,这里排序利用了,对原始数据进行取反存入,保证了从大到小。最后根据map的记录下标进行插值
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
vector<pair<int, int>>arr;
int cnt = 0;
vector<string>str(score.size());
string desc[3] = { "Gold Medal", "Silver Medal", "Bronze Medal" };
for (int i = 0; i < score.size(); i++)
{
arr.emplace_back(make_pair(-score[i], i));
}
sort(arr.begin(), arr.end());
for (auto i = 0; i < score.size(); i++)
{
if (i >= 3) {
str[arr[i].second] = to_string(i + 1);
}
else {
str[arr[i].second] = desc[i];
}
}
return str;
}
};
边栏推荐
- F - Spices(线性基)
- Lihongyi, machine learning 6 Convolutional neural network
- Dirvish Chinese document of vim
- 20年ICPC澳门站L - Random Permutation
- Transformers Roberta如何添加tokens
- 爱
- Expressing the transformation of two coordinate systems with vectors
- Please check the list of commonly used software testing tools.
- vie的刷新机制
- Computer wechat user picture decoded into picture in DAT format (TK version)
猜你喜欢
支付宝被风控7天怎么办?付解决方案
random list随机生成不重复数
AI clothing generation helps you complete the last step of clothing design
Lihongyi, machine learning 6 Convolutional neural network
Yarn: unable to load file c:\users\xxx\appdata\roaming\npm\yarn PS1 because running scripts is prohibited on this system
DSPACE设置斑马线和道路箭头
How transformers Roberta adds tokens
Introduction to database system
AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"
MATLAB主窗口与编辑器窗口分开为两个界面的解决办法
随机推荐
Network planning | [four network layers] knowledge points and examples
Introduction to database system
AI clothing generation helps you complete the last step of clothing design
random list随机生成不重复数
MySql安装教程
自动化测试
Easy to use dictionary -defaultdict
Add in cmakelists_ Definitions() function
ACM. HJ70 矩阵乘法计算量估算 ●●
計網 | 【四 網絡層】知識點及例題
AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"
How to click DOM to automatically locate the corresponding code line in vscode
打新债100%中签的方法 开户是安全的吗
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
How transformers Roberta adds tokens
如何卸载cuda
New solution of 202112-2 sequence query
调用系统函数安全方案
Enlightenment of using shadergraph to make edge fusion particle shader
ACL access control of squid proxy server