[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 ...
随机推荐
- Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦
个人博客 对于技术人员来说,拥有自己的个人博客应该是一件令人向往的事情,可以记录和分享自己的观点,想到这件事就觉得有意思,但是刚开始写博客的时候脑海中是没有搭建个人博客这一想法的,因为刚起步的时候连我 ...
- python通过http请求发送soap报文进行webservice接口调用
最近学习Python调用webservice 接口,开始的时候主要采用suds 的方式生产client调用,后来发现公司的短信接口采用的是soap报文来调用的,然后开始了谷歌,最后采用httplib ...
- 最全DOS的CMD命令,程序员必会
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr. ...
- 最简单 iText 的 PDF 生成方案(含中文解决方案)HTML 转为 PDF
转自:http://my.oschina.net/sanji/blog/277704 最近正好项目有用到 ITEXT ,在网络上搜索了一番,发现了很多方案,但是感觉对于一般开发来说都太复杂了,本文提供 ...
- (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-1
Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,这里一起回顾一下Protobuf 的优点.(1)在谷歌内部长期使用,产品成熟度高:(2)跨语言,支持 ...
- 基于Spring Boot的图片上传
package com.clou.inteface.domain.web.user; import java.io.File; import java.io.IOException; import j ...
- SmartCoder每日站立会议10
站立会议内容: 准备为上交第一阶段项目进行加班,将各个页面联系起来,静态地图变为动态转换,考虑地图全屏或者是小屏即消息展示方式 1.站立会议照片: 2.任务展板: 3.燃尽图:
- 这 5 个前端组件库,可以让你放弃 jQuery UI
欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ 在建立Web应用时,通常都需要用到一些有用的UI组件.无论应用中需要的是日历,滑块,图形或其它用于提升或简化用户交互的组件,那么 ...
- 解决shiro和quartz2 版本冲突问题
修改build.gradle compile ("org.quartz-scheduler:quartz:2.2.3") compile ("org.apache.s ...
- idea的一揽子工程
我总觉得,世上如果人人都像我一样,那路该多难走.有时候在网上找到一些问题的答案,成功解决之后,就这么过去了,实在罪过. 将idea的几个常见的使用问题综合到一起吧,如果有不会用的,欢迎留言.当然,请支 ...