506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。
(注:分数越高的选手,排名越靠前。)
示例 1:
输入: [5, 4, 3, 2, 1]
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
提示:
N 是一个正整数并且不会超过 10000。
所有运动员的成绩都不相同。
详见:https://leetcode.com/problems/relative-ranks/description/
C++:
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums)
{
int n = nums.size(), cnt = 1;
vector<string> res(n, "");
map<int, int> m;
for (int i = 0; i < n; ++i)
{
m[nums[i]] = i;
}
for (auto it = m.rbegin(); it != m.rend(); ++it)
{
if (cnt == 1)
{
res[it->second] = "Gold Medal";
}
else if (cnt == 2)
{
res[it->second] = "Silver Medal";
}
else if (cnt == 3)
{
res[it->second] = "Bronze Medal";
}
else
{
res[it->second] = to_string(cnt);
}
++cnt;
}
return res;
}
};
参考:http://www.cnblogs.com/grandyang/p/6476983.html
506 Relative Ranks 相对名次的更多相关文章
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- Leetcode506.Relative Ranks相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌"(" ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode] 506. Relative Ranks_Easy tag: Sort
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
随机推荐
- Mac OS用vmvare安装多节点kubernetes
参考网址 https://kubernetes.io/docs/setup/ 1.安装vmvare 2.下载ubuntu镜像(可以不要界面,可以下载server版大约900M,否则下载desktop版 ...
- js modify local file
https://stackoverflow.com/questions/4561157/is-it-possible-to-modify-a-html-file-from-which-the-scri ...
- 学习html5 中的canvas(一)
1.canvas画直线 <!doctype html> <html> <head> <meta charset="UTF-8"> & ...
- xunit inlinedata classdata memberdata
https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdat ...
- INFO: Ignoring response <403 https://movie.douban.com/top250>: HTTP status code is not handled or not allowed
爬取豆瓣电影top250,出现以下报错: 2018-08-11 22:02:16 [scrapy.core.engine] INFO: Spider opened 2018-08-11 22:02:1 ...
- IntelliJ IDEA 运行错误:java: Compilation failed: internal java compiler error
错误:java: Compilation failed: internal java compiler error 解决的方法: 文件 --> 设置 : 若没有模块,点击右边的把自己项目的模块添 ...
- kafka 查询 SQL Query
. SELECT COUNT(*) FROM wiseweb_crawler_foreignmedias WHERE site_id= AND (gathertime BETWEEN '2017-05 ...
- 第五届蓝桥杯C++B组省赛
1.啤酒和饮料 2.切面条 3.李白打酒 4.史丰收速算 5.打印图形 6.奇怪的分式 7.六角填数 8.蚂蚁感冒 9.地宫取宝 10.小朋友排队
- UUIDUtils
package com.cc.hkjc.util; import java.util.UUID; /** * 字符串工具类 * * @author:匿名 * */public class UUID ...
- I.MX6 boot from Micro SD
/***************************************************************************** * I.MX6 boot from Mic ...