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]的更多相关文章

  1. 【leetcode】506. Relative Ranks

    problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...

  2. 【LeetCode】506. Relative Ranks 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...

  3. [LeetCode&Python] Problem 506. Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  4. 506. Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  5. [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 ...

  6. 506 Relative Ranks 相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...

  7. LeetCode 506. 相对名次(Relative Ranks) 39

    506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...

  8. [LeetCode] Relative Ranks 相对排名

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  9. LeetCode Relative Ranks

    原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...

  10. [Swift]LeetCode506. 相对名次 | Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

随机推荐

  1. 使用Browserify来实现CommonJS的浏览器加载

    前面的话 Nodejs的模块是基于CommonJS规范实现的,可不可以应用在浏览器环境中呢? var math = require('math'); math.add(2, 3); 第二行math.a ...

  2. VMTools安装

    先启动CentOS并成功登陆如下图,发现底部提示,准备安装 2.选择虚拟机菜单栏--安装VMware tools 3.光驱目录中拷贝VMwareTools-10.0.5-3228253.tar.gz到 ...

  3. 为Android内核添加新驱动

    转载地址:http://blog.chinaunix.net/uid-16759545-id-4892379.html 1. 在drives目录下添加hello目录,内含hello.c Kconfig ...

  4. html5,js插件实现手机端实现头像剪切上传

    思路:先打开相册,选取图片,在剪切图片,转化为base64格式,然后上传到七牛存储,返回url,再传给后端,整个流程就是这样.用的是angular框架,图像插件用到imagecropper.js,废话 ...

  5. Junit 入门使用教程

    1.Junit 是什么? JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个JU ...

  6. .Net Core应用搭建的分布式邮件系统设计

    本篇分享的是由NetCore搭建的分布式邮件系统,主要采用NetCore的Api和控制台应用程序,由于此系统属于公司的所以这里只能分享设计图和一些单纯不设计业务的类或方法: 为什么要在公司中首例采用N ...

  7. springboot 集成elasticsearh的简单配置

    添加依赖 gradle compile("org.springframework.boot:spring-boot-starter-data-elasticsearch:${springBo ...

  8. 快速傅里叶变换(FFT)算法【详解】

    快速傅里叶变换(Fast Fourier Transform)是信号处理与数据分析领域里最重要的算法之一.我打开一本老旧的算法书,欣赏了JW Cooley 和 John Tukey 在1965年的文章 ...

  9. C/C++遍历目录下的所有文件(Windows/Linux篇,超详细)

    本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6815924.html. 前面的一篇文章我们讲了用Windows API遍历一个目录下的所有文 ...

  10. [转]tomcat部署

    转载博客原文地址:  http://www.cnblogs.com/xing901022/p/4463896.html 阅读目录 介绍 静态部署——在tomcat启动时部署 动态部署——在tomcat ...