第十周leetcode题
来源:互联网 发布:大耳朵英语软件 编辑:程序博客网 时间:2024/06/06 09:17
Description:
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".
Example 1:
Input: [5, 4, 3, 2, 1]Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal".
For the left two athletes, you just need to output their relative ranks according to their scores.
Note:
- N is a positive integer and won't exceed 10,000.
- All the scores of athletes are guaranteed to be unique.
将给出的成绩从大到小排序,前三名分别获得金银铜牌,其他则输出位次。注意函数返回的是string类型的vector。用一个int数组复制vector里的数字,排序。再用另一个数组记下排序的结果。最后顺序检索原向量中的元素,输出它们对应的奖牌或位次。代码如下:
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums) {
int num[nums.size()];
vector<string> a;
a.resize(nums.size());
for(int i=0;i<nums.size();i++)
{
num[i]=nums[i];
}
sort(num, num+nums.size(),greater<int>());
int ran[num[0]+1]={0};
for(int i=0;i<nums.size();i++)
{
ran[num[i]]=i+1;
}
for(int i=0;i<nums.size();i++)
{
if(ran[nums[i]]==1)a[i]="Gold Medal";
else if(ran[nums[i]]==2)a[i]="Silver Medal";
else if(ran[nums[i]]==3)a[i]="Bronze Medal";
else a[i]=to_string(ran[nums[i]]);
}
return a;
}
};
- 第十周leetcode题
- leetcode-python 第十周
- leetcode-第十周
- [leetcode]第十周作业
- 第十周LeetCode
- LeetCode题解 第十周
- 第十四周leetcode题
- leetcode【第十周】数字组合
- leetcode第十周解题总结
- 第十周LeetCode算法题两道
- Leetcode 算法习题 第十周
- 第十周:[leetCode] 72. Edit Distance
- 第十周:[Leetcode]486. Predict the Winner
- leetcode算法课程第十周博客
- 每天一题LeetCode[第十天]
- 每天一题LeetCode[第十一天]
- 每天一题LeetCode[第十三天]
- 每天一题LeetCode[第十五天]
- JQuery的事件之鼠标事件
- bootstrap精简教程
- 51Nod-1341-混合序列
- 打印机经常遇到的一些问题
- Xamarin.android 抽屉效果(SlideMenu)
- 第十周leetcode题
- OpenGL学习笔记
- 使用python一键登录博客
- Kibana displays customized percentage in pie chart
- Alphabet Cookies
- MySQL数据库(19)
- Codeforces Round #411 (Div. 2)
- Bootstrap <第一篇>
- LeetCode:Sort List