[leetcode-506-Relative Ranks]
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.
思路:
思路很朴素,首先就是排序,然后定义一个map,储存对应分数及其排名信息。最后再遍历一遍原来的分数数组,对应每一个分数
给定相应的排名。注意,要提前备份原来的数组,因为排序之后数组顺序就变了。
vector<string> findRelativeRanks(vector<int>& nums)
{
vector<int>backup = nums;
vector<string>result;
if(nums.size()==)return result;
sort(nums.begin(),nums.end(),greater<int>());
map<int,string>table;
char rank[];
for(int i = ;i < nums.size();i++)
{
sprintf(rank,"%d",i+);
if(i == )table[nums[i]]="Gold Medal";
else if(i == )table[nums[i]]="Silver Medal";
else if(i == )table[nums[i]]="Bronze Medal";
else table[nums[i]] = rank;
}
result.push_back(table[backup[]]);
for(int i =;i<nums.size();i++)
{
result.push_back(table[backup[i]]);
}
return result;
}
[leetcode-506-Relative Ranks]的更多相关文章
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 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_Easy tag: Sort
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode Relative Ranks
原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
随机推荐
- 实现ThreadFactory接口生成自定义的线程给Fork/Join框架
Fork/Join框架是Java7中最有趣的特征之一.它是Executor和ExecutorService接口的一个实现,允许你执行Callable和Runnable任务而不用管理这些执行线程.这个执 ...
- Hive的分区操作~~~~~~
一.Hive分区(一).分区概念:为什么要创建分区:单个表数据量越来越大的时候,在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据, ...
- Java调用IDL出错处理
之前有一个java调用idl的详细介绍http://www.cnblogs.com/lizhishan3380/p/4353286.html,里面有提到[需要先在java中加载IDL的java包(ja ...
- PHP如何与搜索引擎Elasticsearch交互?
一:参考官方文档 1. Elasticsearch 5.4.0英文手册:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/sear ...
- 《Android进阶》之第四篇 ViewPagerIndicator的使用
1.先将这个开源框架下载到本地: Administrator@QH-20141231RFQJ /d/hixin $ cd ViewPagerIndicator/ Administrator@QH-20 ...
- .Net程序员学用Oracle系列(30):零碎补充、最后总结(The End)
1.同义词 2.Flashback 技术 3.连接字符串的写法 4.转义字符 & 特殊运算符 5.文件类型 6.查看参数 & 修改参数 7.AWR 工具 8.学习方法 & 学习 ...
- 011一对一 唯一外键关联映射_单向(one-to-one)
² 两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ² 有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...
- laravel实现多数据库连接配置
只需三步,便可实现. 第一步,在.env文件中配置 DB_HOST=localhost DB_DATABASE=test DB_USERNAME=root DB_PASSWORD=root DB_HO ...
- 打开Eclipse弹出“No java virtual machine was found..."的解决方法
今天准备用Eclipse抓取Android应用崩溃log,打开Eclipse时发现运行不了有以下弹框 A Java Runtime Environment(JRE) or Java Developme ...
- GPU编程--宏观理解篇(1)
GPU编程与CPU编程最大的不同可以概括为以下两点: "The same program is executed on many data elements in parallel" ...